Intermediate Arrays

Anonymous arrays

Java 2 added anonymous arrays, which allow you to create a new array of values anywhere in the program, not just in an initialization in a declaration.

// This anonymous array style can also be used in other statements.

String[] days = new String[] {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};

You can also use anonymous array syntax in other parts of the program. For example,

// Outside a declaration you can make this assignment.

x = new String[] {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};

You must be careful not to create these anonymous arrays in a loop or as local variables because each use of new will create another array.

Dynamic allocation

Because arrays are allocated dynamically, the initialization values may arbitrary expresssions. For example, this call creates two new arrays to pass as parameters to drawPolygon.

g.drawPolygon(new int[] {n, n+45, 188}, new int[] {y/2, y*2, y}, 3);

C-style array declarations

Java also allows you to write the square brackets after the variable name, instead of after the type. This is how you write array declarations in C, but is not a good style for Java. C declaration syntax can get very ugly with part of the declaration before the variable, and part after. Java has a much nicer style where all type information can be written together without the use of a variable, and there are times when only the Java notation is possible to use.

   int[] a;   // Java style -- good

   int a[];   // C style -- legal, but not Java style

Converting between arrays and Collections data structures


Common array problems

Some common array programming mistakes are:

  • Forgetting that array subscripts start with zero.
  • Writing a.length() instead of a.length. The length() method is used with Strings, not arrays.
  • Declaring an array with a size. Eg, int[100] a; instead of int[] a = new int[100];.

Library methods for arrays

Static methods for manipulating arrays are available in the java.util.Arrays class.


Returns a List based on the array.


Returns a readable form of the array.


Performs binary serach of a sorted array.


Compares two arrays for equality.


Fills entire array or subrange with a value.


Sorts an array.

In addition there is System.arrayCopy() method.

Anonymous arrays have to be assigned to the variables at the time of declaration of varaible itself. Assigning to the variable after variable declaration is not possible 
int[] i = {1,2,3} is fine. 
int[] i; 
i = {1,2,3} is compiler error. 
This restriction at the time of method invocation is valid, since we don't know the type of array(overloading issue). But for assignment the left side variable type is known. So, why should it be not possible to cast the anonymous array {1,2,3} to int[] and assign to variable.

i=i++ produces the output "0″ instead of "1″.

The code 
int i = 0; 
i = i++; 
produces the output "0″ instead of "1″.

"i = i++" roughly translates to

int oldValue = i; 
i = i + 1; 
i = oldValue;

Array and ArrayList

In a arrayList of object, any types of object(mixed) can be put. However in an array of objects, once initialized to a particular object type, other type of objects cannot be put.


ArrayList a = new ArrayList(); 

is allowed and will run fine.


Object[] obj = new String[5]; 
obj[0] = new Integer(1);

will throw ArrayStoreException(is a RunTimeException).

With generics, arraylist is also not allowed to store Integer if it is declared as ArrayList of String. Thus, with generics, 
ArrayList<String> a = new ArrayList<String>(); 
a.add(5);  — will be compiler error(unlike array which is at rutime).

But with generics also, one more difference is array  allows polymorphic assignment., i.e.,

Number[] n = new Integer[5];

However, in case of generics, polymorphic assignment holds good only for the base

type and not for the generic type. i.e.,

List<Integer> l = new ArrayList<Integer>;  — for base type hold good

ArrayList<Number> n = new ArrayList<Integer>; — not allowed for generic type


Anonymous said...


Post a Comment