Arrays

This chapter introduces arrays, a common multi-dimensional data structure used to store data of the same type. (Note that a class is a "data type" in this sense.) Arrays have some number of dimensions and a number of elements in each dimension. These are established when the array is created. The range of the array's index in each dimension goes from zero to the number of that dimension's elements minus one. A for loop is commonly used to initialize, manipulate, and access the values in an array. Here, we treat one-dimensional arrays, sometimes called vectors.

15. End of the Chapter

Answer:

class ArrayEg4
{
  public static void main ( String[] args )
  {
    int[] valA = { 12, 23, 45, 56 };

    int[] valB = new int[4]; 

    valB[ 0 ]  = valA[ 3 ] ;
    valB[ 1 ]  = valA[ 2 ] ;
    valB[ 2 ]  = valA[ 1 ] ;
    valB[ 3 ]  = valA[ 0 ] ;

   }
}

End of the Chapter

Here is a list of facts about arrays. You may wish to refer back to a page that discusses a particular fact in greater detail.

  1. array An array is an object that has room for several values, all of the same type.

  2. Each value is stored in a cell of the array. The values stored in an array are sometimes called the elements of the array.

  3. If there are N cells in the array, the cells are indexed from 0 up to (N-1).

  4. The index must be an integer value (byte, short, or int).

  5. array, declaration An array declaration looks like:

        int[] intArray;
        

    This declaration declares the array reference intArray. It does not create the actual object.

  6. An array can be declared and constructed in a combined statement:

        int[] intArray = new int[17];
        

    This declaration declares the array reference intArray, and constructs an array object containing 17 cells that can hold int.

  7. When an array object is constructed using the new operator, the cells are array, default initialization initialized to the default value of the type of the cell. Numeric types are initialized to zero.

  8. Once an array object has been constructed, the number of cells it has can not be changed. (However, a completely new array object, with a different number of cells, can be constructed to replace the first array object.)

  9. subscripted variable A subscripted variable such as intArray[12] can be used anywhere an ordinary variable of the same type can be used.

  10. The index used with an array can be stored in a variable, for example

        int j = 5 ;
        intArray[ j ] = 24;  // same as: intArray[ 5 ] = 24
        
  11. The index used with an array can be computed in an expression, for example

        int j = 5 ;
        intArray[ j*2 + 3 ] = 24;  // same as: intArray[ 13 ] = 24
        
  12. array, bounds checking The index used with an array must be within the range 0..(N-1) where N is is number of cells of the array.

  13. If an index that is out of bounds is used with an array, an exception is thrown and the program stops running (unless it catches the exception.)

  14. An array can be declared, constructed, and initialized using an array, initializer list initializer list. This can only be done when the array is first declared.

Arrays can be confusing at first. But they are very important. If you are somewhat uncertain about arrays, take a break. Then, sometime later, come back to this chapter and work through it again.