Arrays in java - Declare, Initialize, display, add values and iterate on 1D(One Dimensional), 2D (Two Dimensional) and jagged (Ragged) Arrays



In this tutorial we will learn how to declare and initialize array and add values in it in different ways in java.


Contents of page >






1) 1D - One dimensional Array in java>


1.0) What are Arrays in java?
An array is an object in java that holds a fixed number of values of a single type. Once array is created is length cannot is fixed (i.e. can’t be changed).


1.1) Declare, Initialize and add value in 1D (One dimensional) Array in single line (String Array)
           String string1dArray[] = new String[] { "a", "b", "c" };
Or
          String[] string1dArray = new String[] { "a", "b", "c" };
Or
          String string1dArray[] = { "a", "b", "c" };




Declare, Initialize and add value in 1D Array in single line (int Array )
            int int1dArray[] = new int[] { 1, 2, 3 };


1.2) Declare, Initialize and add value in 1D Array in java.
            //Declare and initialize 1d - which can hold 3 Strings
          String string1dArray[] = new String[3];

                     
          //Now, let's put values in 1D array
          string1dArray[0] = "a";
          string1dArray[1] = "b";
          string1dArray[2] = "c";


1.2.1) Example of illegal declaration of 1D array in java
String string1dArray[] = new String[]; //Compilation error


1.3) Iterate over 1d (One dimensional)  array > using enhanced for loop
          // Way 1 - To Iterate over 1d array > using enhanced for loop
          for (String str : string1dArray) {
                 System.out.print(str+ " ");
          }


1.4) Iterate over 1d (One dimensional)  array > using for loop in java
          // Way 2 Iterate over 1d array > using for loop
          for (int i = 0; i < string1dArray.length; i++) {
                 System.out.print(string1dArray[i]+ " ");
          }


1.5) Full program to declare, Initialize, add value and Iterate on 1D (One dimensional) Array >
public class Array1DExample {
   public static void main(String[] args) {
          // Declare and initialize 1D - One dimensional Array
          String string1dArray[] = new String[] { "a", "b", "c" };
          // Way 1 - To Iterate over 1d array > using enhanced for loop
          for (String str : string1dArray) {
                 System.out.print(str);
          }
   }
}
We declared, Initialized, added value and then iterated on array to display it in java.


Output >
abc


2) 2D - Two dimensional Array / Multi-dimensional Array in java>


2.0) What are 2D arrays in java?
2D arrays are also called Matrix in java. Let’s learn about them with examples.


2.1) Declare, Initialize and add value in 2D (Two dimensional) Array in single line
          // It will for form 2d Array of 2x3 dimensions - i.e. 2 rows and 3 columns
          int int2dArray[][] = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
Or
          int[][] int2dArray = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
Or
          int[] int2dArray[] = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
Or
          int int2dArray[][] = { { 1, 2, 3 }, { 4, 5, 6 } };


2D array will look like this >
1 2 3
4 5 6


2.2) Declare, Initialize and add value in 2D Array in java.
          //Declare and initialize 2d Array of 2x3 dimensions - i.e. 2 rows and 3 columns
          int int2dArray[][] = new int[2][3];

           //Now, let's put values in 2D array
          int2dArray[0][0] = 1;  // Row 0 - Column 0
          int2dArray[0][1] = 2;  // Row 0 - Column 1
          int2dArray[0][2] = 3;  // Row 0 - Column 2
         
          int2dArray[1][0] = 4;  // Row 1 - Column 0
          int2dArray[1][1] = 5;  // Row 1 - Column 1
          int2dArray[1][2] = 6;  // Row 1 - Column 2
2D array will look like this >
1 2 3
4 5 6


2.2.1) Example of illegal declaration of 2D array
int int2dArray[][] = new int[][]; //Compilation error

2.3) Iterate over 2d (Two dimensional)  array > using enhanced for loop
           // Method 1 - To Iterate over 2d array > using enhanced for loop
          for (int[] i : int2dArray) {
                 for (int j : i) {
                       System.out.print(j + " ");
                 }
                 System.out.println();
          }


2.4) Iterate over 2d (Two dimensional)  array > using for loop  in java
          // Method 2 Iterate over 2d array > using for loop
          for (int i = 0; i < int2dArray.length; i++) {
                 for (int j = 0; j < int2dArray[i].length; j++)
                       System.out.print(int2dArray[i][j] + " ");
                 System.out.println();
          }


2.5) Full program to declare, Initialize, add value and Iterate on 2D (Two dimensional) Array >
public class Array2DExample {
   public static void main(String[] args) {
           // Declare and initialize 2D Array
          int int2dArray[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
          // Method 1 - To Iterate over 2d array > using enhanced for loop
          for (int[] i : int2dArray) {
                 for (int j : i) {
                       System.out.print(j + " ");
                 }
                 System.out.println();
          }
   }
}
We declared, Initialized, added value and then iterated on array to display it in java.


Output >
1 2 3
4 5 6

3) Jagged arrays/ Ragged arrays in java>


3.0) What are Jagged arrays in java?
Jagged arrays are those arrays in which each row can have variable size of columns. Let’s see how with examples.


3.1) Declare, Initialize and add value in Jagged arrays/ Ragged array in single line
           // It will for form 2d Jagged Array
            // Row 0 row will have 2 columns And
            // Row 1 row will have 3 columns.
            int intJaggedArray[][]={ { 1, 2}, { 4, 5, 6 } };
Or
          int[][] intJaggedArray={ { 1, 2}, { 4, 5, 6 } };
Or
          int[] intJaggedArray[] ={ { 1, 2}, { 4, 5, 6 } };


Jagged array will look like this >
1 2
4 5 6


3.2) Declare, Initialize and add value in Jagged arrays in java.
Point 1)        Define only ROW    size at time of initialization -
Point 2)  DON’T Define      COLUMN size at time of initialization -
          int intJaggedArray[][] = new int[2][];
//Point 3)  Now, let’s define COLUMN at runtime
          intJaggedArray[0] = new int[2];   // Row 0 will have 2 Columns
          intJaggedArray[1] = new int[3];   // Row 1 will have 3 Columns
//Point 4)  Now, put values in jagged array
          intJaggedArray[0][0] = 1;  // Row 0 - Column 1
          intJaggedArray[0][1] = 2;  // Row 0 - Column 2
          intJaggedArray[1][0] = 4;  // Row 1 - Column 0
          intJaggedArray[1][1] = 5;  // Row 1 - Column 1
          intJaggedArray[1][2] = 6;  // Row 1 - Column 2
Jagged array will look like this >
1 2
4 5 6


3.2.1) Example of illegal declaration of Jagged array
int intJaggedArray[][] = new int[][]; //Compilation error
Or
int intJaggedArray[][] = new int[][3]; //Compilation error


3.3) Iterate over Jagged array in java > using enhanced for loop in java
            // Method 1 - To Iterate over Jagged arrays/ Ragged arrays > using enhanced for loop
         
          for (int[] i : intJaggedArray) {
                 for (int j : i) {
                       System.out.print(j + " ");
                 }
                 System.out.println();
          }


3.4) Iterate over Jagged array in java > using for loop in java
          // Method 2 Iterate over Jagged arrays/ Ragged arrays > using for loop
          for (int i = 0; i < intJaggedArray.length; i++) {
                 for (int j = 0; j < intJaggedArray[i].length; j++)
                       System.out.print(intJaggedArray[i][j] + " ");
                 System.out.println();
          }


3.5) Full program to declare, Initialize, add value and Iterate on 1D (One dimensional) Array >
public class ArrayJaggedExample {
   public static void main(String[] args) {
          int intJaggedArray[][]={ { 1, 2}, { 4, 5, 6 } };
          // Method 1 - To Iterate over Jagged arrays/ Ragged arrays > using enhanced for loop
          for (int[] i : intJaggedArray) {
                 for (int j : i) {
                       System.out.print(j + " ");
                 }
                 System.out.println();
          }
   }
}
We declared, Initialized, added value and then iterated on array to display it in java.


Output >
1 2
4 5 6



Learn how to use Anonymous inner class / double brace initialization to declare, Initialize and add value in ArrayList in single line.
And Arrays.asList to Initialize and add value in List in single line. xxx

Summary >
So, in this core java tutorial we learned how to Declare (all the possible ways to declare an array in java), initialize, displayed and added values in 1D, 2D and jagged Arrays in java with examples and programs.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


Inner class/ nested class, static, local and anonymous inner class in java

ArrayList in java




Must read for you :