Friday, January 15, 2016

Topic 7: Arrays in Java

  • An array is a group of same type variables referred by a common name

  • Arrays can be of any type with one or more dimensions. 

  • A specific element in an array is accessed by its index.

 

One-Dimensional Arrays


  • The general form of a one-dimensional array declaration is: 

type var-name[ ];

Here, type is the data type of element that the array contains. For ex, “array of int”:

int intArray[];

This declaration states that intArray is a int array variable with value set to null.

  • To actually create a physical array we use new  which is a special operator that allocates memory. The general form of array allocation using new as it applies to one-dimensional arrays appears as follows:

array-var = new type[size];

Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and array-var is the array variable that is linked to the array. The elements in the array allocated by new will automatically be initialized to zero. For ex, the following statement creates a physical array with 10-element array of integers and links it to intArray:

intArray = new int[10];

  • After allocation we can access a specific element in the array by specifying its index within square brackets. All array indexes start at zero. For ex, this statement assigns the value 200 to the last element of intArray

intArray[9] = 200;

  • We can combine the declaration and allocation of the array as shown here: 

int intArray[] = new int[10];

  • Arrays can be initialized when they are declared. An array initializer is a list of comma-separated expressions surrounded by curly braces. The array will automatically be created large enough to hold the number of elements we specify in the array initializer. There is no need to use new. For ex:

int intArray[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };

 

Multidimensional Arrays


  • Multidimensional arrays are actually arrays of arrays.

  • To declare a multidimensional array variable, specify each additional index using another set of square brackets. For ex, the following declares and allocates a two-dimensional array:

int twoDim[][] = new int[4][5];

This allocates a 4 by 5 array and assigns it to twoDim. Internally this matrix is implemented as an array of arrays of int.

  • When we allocate memory for a multidimensional array, we need only specify the memory for the first (leftmost) dimension. We can allocate the remaining dimensions separately and also with different values, if required. Consider the following code snippet which declares and allocates a 2 by 2 int array and individually assign different values for second dimension:

int twoDim[][] = new int[2][];

twoDim[0] = new int[4];

twoDim[1] = new int[3];

  • We can initialize multidimensional arrays by enclosing each dimension’s initializer within its own set of curly braces. For ex,  

int twoDim[][] = { { 0*0, 1*0, 2*0, 3*0 }, { 0*1, 1*1, 2*1 }, { 0*2, 1*2 } };

The following creates a 3 by 4 by 5, three-dimensional array:

int threeDim[][][] = new int[3][4][5];


Alternative Array Declaration Syntax


  • We can also declare an array as shown:

type[ ] var-name;

Here, the square brackets follow the type specifier, and not the name of the array variable. For ex, the following two declarations are equivalent:

int a[] = new int[3];

int[] a = new int[3];

The following declarations are also equivalent:
 

char twod[][] = new char[3][4];
 

char[][] twod2 = new char[3][4];

  • This alternative declaration form:

  • Offers convenience when declaring several arrays at the same time. For ex,
     

//Creates 3 arrays. Same as: int nums[], nums2[], nums3[];

int[] nums, nums2, nums3;
 

  • Is useful when specifying an array as a return type for a method. For ex,
     

public int[] method()

 
Important note:

  • Java strictly checks to make sure we do not accidentally try to store or reference values outside of the range of the array. During runtime, if we try to access elements outside the range of the array, including negative numbers or numbers greater than the length of the array, Java will throw run-time error.

  • Arrays are implemented as objects. The size of an array—that is, the number of elements that an array can hold—is found in its length instance variable. All arrays have this variable, and it will always hold the size of the array. For ex, consider the following code fragment:

int a1[] = new int[10];

int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};

System.out.println("length of a1 is " + a1.length + "

length of a2 is " + a2.length);

Keep in mind that the value of length has nothing to do with the number of elements that are actually in use. It only reflects the number of elements that the array is designed to hold.

 

Click for NEXT article.


Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment