Tutorials Hut

Java Array

Java array is wrapper class in java which is used to store same type of data in a sequence or list, for example if we want to store names of 10 people we can use string type array to hold list of name.

  • Array stores multiple same type of data in an index.
  • Array index starts with 0 and ends with total length of array -1.
java array

Syntaxes of Java Array

Declaration of Array

<Array type>[] <name of array>;

Initialization of Array

<array variable> = new datatype[size];

Example of Array

public class JavaArrayExample {
public static void main(String[] args) {
String[] strArray; //declaration of array
strArray = new String[5]; // Initialization of string array
strArray[0] = "John";
strArray[1] = "Meena";
strArray[2] = "Mr Tesla";
strArray[3] = "Sandy";
strArray[4] = "Rax";
System.out.println("0 index array element value = "+strArray[0]);
//declaration and initialization with size in one line
String[] strArray2 = new String[5];
}
}

Output

0 index array element value = John

Types Of Array

Types of Arra

Single dimensional array

Single dimensional array stores one type of data element in sequence.

Syntax of Single Dimensional Array

<Array data type>[] <name of array> = new datatype[size];

Example of Single Dimensional Array

public class JavaArrayExample {
public static void main(String[] args) {
     int[] intArray = new int[3]; //single dimensional array
     intArray[0] = 10;
     intArray[1] = 11;
     intArray[2] = 12;
     System.out.println("0 index array element value = "+intArray[0]);
}
}

Output

0 index array element value = 10

Multidimensional Array

Multi-dimensional array stores one type of data element in multiple sequence.

Syntax of Multidimensional Array

[][] arrayVatiableName = new Data type[size][size];

This syntax will create two dimensional array or multidimensional array, see carefully the rectangular [] braces by using 1 we create single dimensional, by using more than 1 we create multidimensional arrays.

Example of Multidimensional Array

public class JavaArrayExample {
public static void main(String[] args) {
int[][] intArray = new int[3][3]; //multidimensional array
intArray[0][0] = 22;
intArray[0][1] = 11;
intArray[0][2] = 12;
intArray[1][0] = 10;
intArray[1][1] = 11;
intArray[1][2] = 12;
intArray[2][0] = 10;
intArray[2][1] = 11;
intArray[2][2] = 12;
System.out.println("[0][0] index array element value = "+intArray[0][0]);
}
}

Output

[0][0] index array element value = 22

For Loop On Array

We can loop through any array and access each element.

Syntax

for( variable type: array name) {

}

Example

public class JavaArrayExample {
public static void main(String[] args) {
//Another way to create array of string
String[] names = { "Rax", "John", "Tom"};
//for each loop on array
for(String name: names){
System.out.println(name);
}
}
}

Output

Rax
John
Tom

References

Next Article To Read

You may be interested to read next articles:














  • Leave a Reply

    Your email address will not be published. Required fields are marked *