Arrays in Java
An Array is a collection of similar data items stored in contiguously on memory.
Arrays are objects in Java that store multiple variables of the same type.
Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements.
Array Declaration:
Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.
Declaring an Array of Primitives :
int ar[ ]; //(work but not recommended) int[ ] ar; // (recommended way)
Array Construction:
Constructing an array means creating the array object on the heap.
To create an array object, Java needs to know how much space to allocate on the heap, so you must specify the size of the array at construction time.
Constructing One-Dimensional Arrays
-
int[ ] ar; // Declaration ar = new int[4]; //construction size is mandatory
When you construct an array,each index get initialized with default value of that type.
The given code creates a new object on the heap - an array object holding four elements - with each element containing an int with the default value 0.
length property :
Every array has a length property that holds the size of array.
Types of Array :
There are two types of array.
1. Single Dimensional Array
2. Multidimensional Array
2. Jagged Array
1. Single Dimensional Array :
Example : simple example of java array, where we are going to declare, instantiate, initialize and traverse an array. class ArrayDemo { public static void main(String []ar) { //declaration and instantiation int a[]=new int[5]; a[0]=10;//initialization a[1]=20; a[2]=30; a[3]=40; a[4]=50; //printing array //length is the property of array for(int i=0;i< a.length;i++) System.out.println(a[i]); } }OUTPUT :
10 20 30 40 50Different ways to Declaration, Instantiation and Initialization of Java Array :
1. int ar []; ar = new int[5]; OR 2. int ar [] = new int[5]; OR 3. int ar [] = {10,20,30}; OR 4. int ar [] = new int[]{10,20,30};
Example :
class ArrayDemo { public static void main(String []ar) { int a[] = {10,20,30}; //printing array for(int i = 0 ; i< a.length ; i++) System.out.println(a[i]); } }OUTPUT :
10 20 30
Passing Array to Method :
Example :
Find sum of array member an array using method, take the input from userimport java.util.Scanner; class ArrayDemo { static void getSum(int ar[]) { int sum = 0; //using for-each loop for(int i : ar) { sum = sum + i; } System.out.println("SUM = "+sum); } public static void main(String []ar) { int i, size; Scanner sc= new Scanner(System.in); System.out.print("Enter size of Array :"); size = sc.nextInt(); int a [] = new int[size]; System.out.print("Enter "+size+" Elements :"); for (i = 0; i< a.length;i++) { a [i] = sc.nextInt(); } getSum(a); } }OUTPUT :
Enter size of Array :3 Enter 3 Elements :1 2 3 SUM = 6
2. Multi Dimensional Array :
-
Multidimensional arrays, are simply arrays of arrays.
-
To declare a multidimensional array variable, specify each additional index using another set of square brackets.
Declaration :
type[][] arrayRefVar; OR type arrayRefVar[][]; OR type []arrayRefVar[];
Construction :
arrayRefVar = new type[size][size]; Example : int myArray [][]; myArray = new int[3][3];
Access / Initialization :
myArray[0][0] = 3; myArray[0][1] = 2; myArray[1][2] = 5; …etc.
Example :
class ArrayDemo { public static void main(String []ar) { //declaring and initializing 2D array int arr[][]={ {1,2,3}, {4,5,6}, {5,7,8} }; //printing 2D array for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } }
OUTPUT :1 2 3 4 5 6 5 7 8
3. Jagged Array :
-
It is a new feature supported by Java.
-
In Jagged arrays, each row, in a two-dimensional array, may contain different lengths.
Declaration : int [][] myArray; Construction : myArray = new int[3][]; Note : Only the first bracket is given size. That is acceptable in Java, since the JVM needs to know only the size of the object assigned to the variable. Initialization : myArray[0] = new int[]{1,2,3}; myArray[1] = new int[]{4,6};
Next topic is strings
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in