Java Arrays
Java Arrays
Array is a collection of similar data-types . Also Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
➤ Declaring an Array
To declare an array, define the variable type with square brackets :
Syntax :-
dataTypevarName[] = {// array elements separated by comma};
Here I am declaring an array of strings.
String[]animals;
As we have declared an array , lets give values to it.
String[]animals= {"Dog", "Elephant", "Fox", "Cow"};
➤ Index of an Array
Index of an Array starts from zero.
We also can declare an array of a given size.
Here I declared an array of animals of length 1 i.e It would have 2 containers , 1 at index 0 and other at index 2.
➤ Change an Array element
You can rewrite or replace an array element by declaring it again at the same index.
public classMyClass{ public static void main(String[] args) { String []animals= new String[1];animals[0]="cat";animals[1]="dog";//changing at index 0animals[0]="hello"; System.out.println(animals[0]); } }
Output:-
hello
➤ Array Length
You can find out the length of an array by using the length property.
public classMyClass{ public static void main(String[] args) { String []animals= {"Cat" , "Dog" , "Elephant"}; System.out.println(animals.length); } }
Output:-
3
➤ Using Loops in Array
( If you are not familiar with loops , check out the JAVA Loops section. )
Any Loops can be used with arrays.
Here we are using for loop :
public classMyClass{ public static void main(String[] args) { String[]animals= { "Cat", "Dog", "Elephant" }; for (inti= 0;i<animals.length;i++) { System.out.println(animals[i]); } } }
Output:-
Cat
Dog
Elephant
➤ Enhanced For Loop (For Each Loop)
Syntax:-
for (dataType varName : arrayName) {
// block of code
} Flowchart:-
For Each Loop is also called as enhanced for loop.
The overall working is similar , but For Each loop is easier to write and more human readable.
Using For Each Loop in the above program.
public class MyClass {
public static void main(String[] args) {
String[] animals = { "Cat", "Dog", "Elephant" };
for (String i : animals)
{
System.out.println(i);
}
}
} This will generate the same output as above.
➤ Multi-Dimensional Arrays
Multi-Dimensional Arrays are Arrays within Arrays i.e An Array containing another Array.
Here we will learn about Two-Dimensional Arrays.
These multi-dimensional arrays can be understood by seeing it as a Matrix.
For 2d arrays it can be understood as a 2x2 Matrix.
Similarly for 3d arrays it will 3x3 Matrix.
Let us consider a 2d array of type int :-
int [][] myNumbers = { {1,2,3} , {4,5,6} }; For accessing an element of a particular array:-
System.out.println(myNumbers[1][2]); Here the first [ ] represents the number of array , we chose 1 i.e the 2nd array as its index starts from zero .
And the second [ ] represents the array element in it , we chose 2 i.e 3rd element as its index starts from zero.
The above code would display result as :-
6 As 6 is the 3rd element of 2nd array.
The 3d array works in similar way. You can take help of the following image.




