arrays

 

          see also Arrays class

 

  Arrays have many forms of legal declares. All of the following will work:

 

int  myarray[ ]; 

int[ ]  myarray; 

int  [ ]myarray; 

int[ ]  [ ]myarray; 

int[ ]  myarray[ ]; 

 

  For multidimensional arrays, the following is a legal declaration:

 

int[ ] myarray[ ] = new int [5] [5];

 

  You cannot put a length onto the left hand side of an array's declaration.

 

int[10] myarray[ ];       will not compile.

int[10] myarray[ 10];    will not compile.

 

  You cannot put a number inside the [ ] when populating the array in its declaration. 

 

int[3] x = { 1, 2, 3 };      will not compile.

int[ ] x = { 1, 2, 3 };       works.

 

  You cannot omit the array's length when you create an array with new.

 

int myarray[ ] = new int[ ];         will not compile. No length.

int myarray[ ] = new int[10];      works.

 

  Determining an array’s length.  You cannot use .length or the getLength(...) method before the array's length has been specified.

 

int myarray = new int[ ]; 

int x = myarray.length;             will not compile. 

 

int myarray[ ] = new int[3]; 

int x = myarray.length;             works.

 

int myarray[ ] = new int[3]; 

int x = java.lang.reflect.Array.getLength(myarray);  also works.

 

  You cannot use the getLength( ) method without doing one of the following:

 (a) using an import like  import java.lang.reflect.Array; or,

 (b) fully qualifying the reference, like   java.lang.reflect.Array.getLength( yourarray );

 

  Certain variables work as array indexes. An array's length index can be an int literal or any variable promotable to int, such as a byte, char, or short. 

 

  Putting a long literal or variable inside the [ ] length index gives a compile error.  i.e.

 

long lg = 10;

int myarray[  ] = new myarray[ lg ];          will not compile.

 

  You cannot change the size of an array once it is created.  Changing its length variable later does not change the size of the array, which by now is fixed.  i.e.

 

int x = 3;                                         then

int myarray[  ] = new int[ x ];      then

x = 20;                                  This statement compiles and runs, but myarray's length stays 3.

 

  You cannot exceed the array's length-1 in the index or you will generate a runtime ArrayIndexOutOfBounds exception.  This is true even if the compiler should miss detecting it.  i.e.

 

String[ ] a = new String[ 3 ];       then

a[ 10 ] = "A";                          compiles despite a's length of 3, but it generates the runtime exception.

 

  You can assign null to an array successfully because arrays are Objects. 

 

int[ ] a = null;             works.

int[ ] b;                    At this point array b has also become null, as this statement is

                              occurring before your later creation statement.

 

  You can use { } for combining array declaration and creation. 

 

int[  ]  x = { 1, 2, 3 };                 works.

int[ ]  x = new int[ ] { 1, 2, 3 };     works.

int[ ]  x{ 1, 2, 3 };                     will not compile, as it omits the = sign.

 

  You cannot leave out terms by employing comma placeholders within the { }.

 

int[ ] x = { 1, 2, , 4 };                 will not compile

An ending comma is allowed however and is ignored.  i.e. 

int[ ] x = { 1, 2, 4, };                  works.

 

  Use double and nested {{ }} with [ ] [ ] multidimensional arrays. 

 

int[ ] [ ] x = { {1,1}, {2,2}, {3,3} }; 

 

  For default initializations, arrays of primitives receive zeroes while arrays of objects (including wrappers) get nulls.

 

long[ ] longarray = long[ 10 ];                will be zeroes

Long[ ] Longarray = Long[ 10 ];             will become nulls.

. . . .

boolean[ ] boolarray = boolean [ 10 ];     will be falses

Boolean[ ] Boolarray = Boolean [ 10 ];     will become nulls.

 

  Arrays get initialized no matter where they occur in a program.  This includes being located inside methods. Arrays declared inside methods are exceptions to the general initialization rules and always get initialized.  See initialization order

 

  The three kinds of things arrays can hold are:

(1) primitive types

(2) object references

(3) references to other arrays (which could be considered part of (2) )

 

  Since command line input parameters supplied to a Java program come in to the main(...) method as elements in an array (usually called args[ ] or argv[ ] ), the length property can be used to determine if any parameters were supplied to the program.  i.e.

 

if ( args.length == 0 ) {

    System.err.println( "Missing command parameter" );

    System.exit(0);

}

if ( args.length < 2 ) {

    System.err.println( "You must have two or more command parameters" );

    System.exit(0);

}

 

   Loading byte arrays from Strings.   You can load a byte array from a String like this:

 

String s = "Hello World";

byte[ ] b = new byte[s.length( )];

b = s.getBytes( );

 

   Printing an Array:

 

System.out.println( a );   prints just the object ID

 

for ( int x = 0; x < a.length;  x++ ) System.out.println( a[ x ] );               works

System.out.println( Arrays.asList( a ));                                                     also works

 

   Creating Arrays from Collections and Maps:

 

import java.util.*;

 

Object[ ] a = yourlist.toArray( );                                  Creates an array containing the elements in a list

                         

                        Object[ ] a = yourset.toArray( );                                  Creates an array containing the elements in a set

                         

                        Object[ ]  a = yourmap.keySet( ).toArray( );           Creates an array containing the keys in a map

                 

                        Object[ ]  a = yourmap.values( ).toArray( );           Creates an array containing the values in a map