+92 332 4229 857 99ProjectIdeas@Gmail.com

Arrays In Java


Arrays In Java:


Suppose we have here three variables of type int with different identifiers for each variable.

            int numberOne;
            int numberTwo;
            int numberThird;

            numberOne = 1;
            numberTwo = 2;
            numberThird = 3;

Code




As you can see, it seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose.
In Java and other programming languages, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. This type of variable is called an array. An array stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots.
 
DECLARING ARRAYS IN JAVA:

     int[] intArray;

   Or,

     int intArray[];
            
   // Now Defining indexes to "5", can store values from 0 to 4
  
     intArray = new int[5]; // setting size of array

   Or,

     int[] intArray = new int[5];



SOME DIRECT INITIALIZING OF DATA IN ARRAY:

boolean[] boolArray = {true,false}; // boolArray contains two elements in it

String[] string = {"Hello","World"}; // string contains two elements in it

     // Declaring integer array empty and with unknown indexes
        
    int[] intArray; // no initialization
    intArray = new int[] {5,4,3,2,1,8,9}; // Contains data now 7 indexes

ACCESSING ARRAY ELEMENTS:

   int[] intArray = {5,4,3,2,1,8,9};
   System.out.println(intArray[3]); // Displays the data of 3rd index starting from 0th

Output:
      2 // because it is at 3rd index

PRINT ALL ARRAY ELEMENTS:


 int[] intArray = {5,4,3,2,1,8,9}; // has 7 indexes so loop would be less than 7 because it is staring from 0
           
  // Displaying intArray all elements on console
  
   for(int i = 0; i < 7 ; i++) {

           System.out.print(intArray[i] + " ");

   }

PRINTING USING length METHOD:


  int[] intArray = {5,4,3,2,1,8,9};

 // Displaying intArray all elements on console using length method
       for(int i = 0; i < intArray.length ; i++) {

            System.out.print(intArray[i] + " ");

       }

Output:
       5 4 3 2 1 8 9

char[] charArray = new char[] {'A','B','C','D','E'};       

     for(int i = 0 ; i < charArray.length ; i++) {

             System.out.print(charArray[i] + " ");
     }

Output:
       A B C D E

The elements of an n-element array have indexes from 0 to n-1. Note that there is no array element, arr[n]! This will result in an array-index-out-of bounds exception.
Remember: You cannot resize an array

MULTIDIMENSIONAL ARRAYS:

-> Multidimensional arrays are implemented as arrays of arrays.
-> Multidimensional arrays are declared by appending the appropriate number of bracket pairs after the array name.

   // [rows][columns] twoDArray

  int[][] twoDArray = new int[3][3]; // means 3 rows and 3 columns

   // Or

  int[][] twoDArray =  {{1,2,3},{4,5,6},{7,8,9}};

       for(int row = 0 ; row < twoDArray.length ; row++) {

            for(int column = 0 ; column < twoDArray.length ; column++ ) {

                          System.out.print(twoDArray[row][column] + " ");
             }

            System.out.println();
        }

Output:
1 2 3
4 5 6
7 8 9        

   // character array
    char[][][] threeD = new char[8][16][24];

   // String array 4 rows 2 columns
     String[][] dogs = {{ "terry", "brown" },{ "Kristin", "white" }
                          ,{ "toby", "gray"},{ "fido", "black"}};

TO ACCESS FIRST ELEMENT OF dogs ARRAY:

   System.out.println(dogs[0][0]); // This will print the String "terry" on the screen.

JAGGED ARRAYS:

Example 1:

// Jagged Arrays

  int[][] triArray;

  // Allocate each part of the two-dimensional array individually.

     triArray = new int[5][];// Allocate array of rows

        for (int row = 0; row < triArray.length; row++) {

                  triArray[row] = new int[row+1];  // Allocate a row

       }

   // Print the Half triangular array
      
            for (int row = 0; row < triArray.length; row++) {

                for (int column = 0; column < triArray[row].length; column++){

                        System.out.print(" " + triArray[row][column]);

                }

               System.out.println("");
            }

Output:

 0
 0 0
 0 0 0
 0 0 0 0
 0 0 0 0 0

Example 2:

int[][] array = {{0,1,2,3,4},
                    {0,1},
                    {0,1,2},
                    {0,1,2,3},
                    {0,1,2,3,4,5},
                    {0},
                    {0,1,2,3},
                    {0,1,2,3}};
        
   for (int i = 0; i < array.length; i++) {

          for (int j = 0; j < array[i].length;j++) {                    

                    System.out.print(array[i][j] + " ");

          }

       System.out.println();

   }

Output:

0 1 2 3 4
0 1
0 1 2
0 1 2 3
0 1 2 3 4 5
0
0 1 2 3
0 1 2 3

0 comments: