SlideShare a Scribd company logo
Arrays in JAVA
 Array is collection of related data items
 Creating an array
 Declare an array
 Create memory location
 Putting values to memory locations
Declaring an Array Variable
 Do not have to create an array while declaring array
variable
 <type> [ ] variable_name;
 Double[ ] myList;
 double myList[ ];
 Both syntaxes are equivalent
 No memory allocation at this point
Syntax:
1. dataType[] arr; (or)
2. dataType []arr; (or)
3. dataType arr[];
Defining an Array
Define an array as follows:
 variable_name=new <type>[arraySize];
 Number = new int[5];
 Mylist = new int[10];
It creates an array using new dataType[arraySize];
 It assigns the reference of the newly created array to
the variable variable_name.
 dataType arrayname[ ] = {list of values};
 Int a [ ]={1,2,3,4,5,6,7,};
 Array index starts from 0 to arraySize-1;
 int is of 4 bytes, total space=4*10=40 bytes
Declaring and defining in the same statement:
Creating arrays cntd...
What happens if we define diffrent type…
 We define
 Int[ ] a=new long[20];
incompatible types
found: long[]
required: int[]
 The right hand side defines an array, and thus the array variable should
refer to the same type of array
Example:
int prime[100];
error=> ']' expected
long primes[20];
 The C++ style is not permitted in JAVA syntax
long[] primes = new long[20];
primes[25]=33;
Runtime Error:Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException
Array Size through Input
….
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
String inData;
int num;
System.out.println("Enter a Size for Array:");
inData = stdin.readLine();
num = Integer.parseInt( inData ); // convert inData to int
long[] primes = new long[num];
System.out.println(“Array Length=”+primes.length);
….
SAMPLE RUN:
Enter a Size for Array:
4
Array Length=4
Example for array
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}
Otput:
1.9
2.9
3.4
3.5
Array Length
 Refer to array length using length() method
 A data member of array object
 array_variable_name.length
 for(int k=0; k<primes.length;k++)
 Sample Code:
long[ ] primes = new long[20];
System.out.println(primes.length);
 Output: 20
 If number of elements in the array are changed,
JAVA will automatically change the length attribute!
Sample Program
class MinArray
{
public static void main ( String[] args )
{
int[] array = { 20, 19, 1, 5, 71, 27, 19, 95 } ;
int min=array[0]; // initialize the current minimum
for ( int index=0; index < array.length; index++ )
if ( array[ index ] < min )
min = array[ index ] ;
System.out.println("The minimum of this array is: " + min );
}
}
*Program taken from: http://chortle.ccsu.edu/CS151/Notes/chap47/ch47_10.html
Two dimenssional array
 Representing 2D arrays
 Int myarray[][];
 Myarray = new int[3][4];
 Int myarray [][] = new int[3][4];
 Example
 Int myarray[2][3]={0,0,0,1,1,1};
2 columns and 3 rows
For more detail contact us

More Related Content

PPT
ARRAYS.ppt
PPT
ARRAYS.ppt
PPT
Array and 2D Array and with syntax working
PPT
Arrays are used to store multiple values in a single variable, instead of dec...
PPT
ARRAYSinJavaProgramming_Introduction.ppt
PPT
ARRAYS in java with in details presentation.ppt
PPT
ARRAYS.ppt
PPTX
Computer programming 2 Lesson 13
ARRAYS.ppt
ARRAYS.ppt
Array and 2D Array and with syntax working
Arrays are used to store multiple values in a single variable, instead of dec...
ARRAYSinJavaProgramming_Introduction.ppt
ARRAYS in java with in details presentation.ppt
ARRAYS.ppt
Computer programming 2 Lesson 13

Similar to ARRAYS.ppt (20)

PPTX
PPTX
Chap1 array
PPTX
Arrays.pptx
PPTX
Chapter 7.1
PPTX
Arrays in Java with example and types of array.pptx
PPTX
Array lecture
PDF
Java arrays (1)
PPTX
Module 7 : Arrays
PDF
Lecture 2.8 Arrays.pdf
PDF
Lecture3pptx_____2024_08_28_22_04_02.pdf
PPTX
Intro to C# - part 2.pptx emerging technology
PPT
Arrays Basicfundamentaldatastructure.ppt
PDF
Arrays a detailed explanation and presentation
PPTX
Arrays in Data Structure and Algorithm
PPT
7.basic array
PDF
Learn Java Part 9
PPT
17-Arrays en java presentación documento
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Chap1 array
Arrays.pptx
Chapter 7.1
Arrays in Java with example and types of array.pptx
Array lecture
Java arrays (1)
Module 7 : Arrays
Lecture 2.8 Arrays.pdf
Lecture3pptx_____2024_08_28_22_04_02.pdf
Intro to C# - part 2.pptx emerging technology
Arrays Basicfundamentaldatastructure.ppt
Arrays a detailed explanation and presentation
Arrays in Data Structure and Algorithm
7.basic array
Learn Java Part 9
17-Arrays en java presentación documento
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Ad

Recently uploaded (20)

PPTX
Glazing at Facade, functions, types of glazing
PPTX
Practice Questions on recent development part 1.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
International Journal of Information Technology Convergence and Services (IJI...
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Top 10 read articles In Managing Information Technology.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPT
Drone Technology Electronics components_1
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
OOP with Java - Java Introduction (Basics)
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Glazing at Facade, functions, types of glazing
Practice Questions on recent development part 1.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
International Journal of Information Technology Convergence and Services (IJI...
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Operating System & Kernel Study Guide-1 - converted.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Top 10 read articles In Managing Information Technology.pdf
Lesson 3_Tessellation.pptx finite Mathematics
CH1 Production IntroductoryConcepts.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Chapter 6 Design in software Engineeing.ppt
Structs to JSON How Go Powers REST APIs.pdf
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Drone Technology Electronics components_1
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
July 2025: Top 10 Read Articles Advanced Information Technology
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Ad

ARRAYS.ppt

  • 2.  Array is collection of related data items  Creating an array  Declare an array  Create memory location  Putting values to memory locations
  • 3. Declaring an Array Variable  Do not have to create an array while declaring array variable  <type> [ ] variable_name;  Double[ ] myList;  double myList[ ];  Both syntaxes are equivalent  No memory allocation at this point Syntax: 1. dataType[] arr; (or) 2. dataType []arr; (or) 3. dataType arr[];
  • 4. Defining an Array Define an array as follows:  variable_name=new <type>[arraySize];  Number = new int[5];  Mylist = new int[10]; It creates an array using new dataType[arraySize];  It assigns the reference of the newly created array to the variable variable_name.  dataType arrayname[ ] = {list of values};  Int a [ ]={1,2,3,4,5,6,7,};  Array index starts from 0 to arraySize-1;  int is of 4 bytes, total space=4*10=40 bytes Declaring and defining in the same statement:
  • 6. What happens if we define diffrent type…  We define  Int[ ] a=new long[20]; incompatible types found: long[] required: int[]  The right hand side defines an array, and thus the array variable should refer to the same type of array Example: int prime[100]; error=> ']' expected long primes[20];  The C++ style is not permitted in JAVA syntax long[] primes = new long[20]; primes[25]=33; Runtime Error:Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
  • 7. Array Size through Input …. BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String inData; int num; System.out.println("Enter a Size for Array:"); inData = stdin.readLine(); num = Integer.parseInt( inData ); // convert inData to int long[] primes = new long[num]; System.out.println(“Array Length=”+primes.length); …. SAMPLE RUN: Enter a Size for Array: 4 Array Length=4
  • 8. Example for array public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } } } Otput: 1.9 2.9 3.4 3.5
  • 9. Array Length  Refer to array length using length() method  A data member of array object  array_variable_name.length  for(int k=0; k<primes.length;k++)  Sample Code: long[ ] primes = new long[20]; System.out.println(primes.length);  Output: 20  If number of elements in the array are changed, JAVA will automatically change the length attribute!
  • 10. Sample Program class MinArray { public static void main ( String[] args ) { int[] array = { 20, 19, 1, 5, 71, 27, 19, 95 } ; int min=array[0]; // initialize the current minimum for ( int index=0; index < array.length; index++ ) if ( array[ index ] < min ) min = array[ index ] ; System.out.println("The minimum of this array is: " + min ); } } *Program taken from: http://chortle.ccsu.edu/CS151/Notes/chap47/ch47_10.html
  • 11. Two dimenssional array  Representing 2D arrays  Int myarray[][];  Myarray = new int[3][4];  Int myarray [][] = new int[3][4];  Example  Int myarray[2][3]={0,0,0,1,1,1}; 2 columns and 3 rows For more detail contact us