Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

November 27, 2011

Anonymous Arrays Code Example

Anonymous Arrays in Java are those arrays that are constructed without specifying the size nor assigned a reference. Anonymous arrays are created on-the-fly when needed and cannot be re-accessed (since there is no reference available to the array object), therefore they become eligible for garbage-collection as soon as the code that uses it completes it.

package info.icontraining.core;

public class AnonymousArrays {

   public static void main(String[] args) {
  
      int[] arr1 = {1, 2, 3, 4, 5};
  
      for(int i: arr1) {
         System.out.print(i);
      }
  
      for(int i: new int[]{1, 2, 3, 4, 5} ) {
         System.out.print(i);
      }  
   }
}

April 1, 2011

Multi-dimensional Arrays in Java

Multi-dimensional arrays are actually arrays of arrays. For example, a 2-dimensional array of type String is really an object that refers to an array, each element of which is referring to another String array.

The second dimension actually refers to the actual String objects.

Example of a 2-dimensional array:

public class Test {
     
   public static void main(String args[]) {
         
      String[][] strArr2d = new String[3][];

      strArr2d[0] = new String[2];
      strArr2d[1] = new String[3];
      strArr2d[2] = new String[4];

      strArr2d[0][0] = "Hello";
      strArr2d[0][1] = "World";

      strArr2d[1][0] = "Thou";
      strArr2d[1][1] = "Art";
      strArr2d[1][2] = "That";

      strArr2d[2][0] = "Truth";
      strArr2d[2][1] = "Shall";
      strArr2d[2][2] = "Always";
      strArr2d[2][3] = "Prevail";
   }
}

Example of a 3-dimensional array:


public class Test {
     
   public static void main(String args[]) {
         
      int[][][] strArr3d = new int[2][][];

      strArr3d[0] = new int[2][];
      strArr3d[1] = new int[2][];

      strArr3d[0][0] = new int[2];
      strArr3d[0][1] = new int[2];
         
      strArr3d[1][0] = new int[2];
      strArr3d[1][1] = new int[2];

      strArr3d[0][0][0] = 1;
      strArr3d[0][0][1] = 2;

      strArr3d[0][1][0] = 3;
      strArr3d[0][1][1] = 4;

      strArr3d[1][0][0] = 5;
      strArr3d[1][0][1] = 6;

      strArr3d[1][1][0] = 7;
      strArr3d[1][1][1] = 8;
   }
}

February 5, 2011

Arrays in Java

Arrays in Java are represented as objects. When an array is constructed all its elements are initialized with defaults values (irrespective of whether the array is declared as an instance variable or local variable).

Constructing an array of object references constructs only the array objects and not the objects themselves.

public class Test {

   private static int[] intArr = new int[3];

   public static void main(String[] args) {

      Test[] testArr = new Test[2];

      for (int i=0; i < testArr.length; i++) {
         System.out.println(testArr[i]);
      }

      for (int j=0; j < intArr.length; j++) {
         System.out.println(intArr[j]);
      }

      intArr[0] = 3;
      testArr[1] = new Test();
   }
}