Here is how you would allocate an array of objects using Java’s short hand syntax:
String[] fruits = { "banana" , "pear" , "orange" , null, favouriteFruit() };
If your array type is declared abstract, you can still do stage 1. However, for stage 2, you need to populate with actual Objects derived from that abstract type.
fruits = new String [] { "banana" , "pear" , "orange" , null, favouriteFruit() };
// note how you never specify the array's size in its type declaration. int[]v; // allocate space for the array v =new int [100]; for ( int i=0;i<v.length;i++ ) { v[i] = i*2 + 999; }Here is how you would allocate an array of objects.
Cell[] v = new Cell [100]; for ( int i=0; i<v.length; i++ ) { v[i] = new Cell( i, 999 ); }A common error to make is to declare an array: e.g.
long[] a;then later to attempt to initialise it with:
long[] a = new long[100];This does not work because you have declared a local stack variable, unrelated to the first one. Make sure you do it like this:
long[] a; // then later initialise it with: a = new long [100];You can initialise an array to a list of values this way:
int[] array = { 11, 12 , 13, 21, 22, 21, 31, 32, 33 };The same technique works with arrays of objects:
BigDate[] dates = { new BigDate( 1999, 1, 1 ), new BigDate( 1999, 12, 31 ) };You might think you could similarly assign a array constant to an array like this:
array = { 11, 12, 13, 21, 22, 21, 31, 32, 33 };However, the syntax needed (introduced with JDK 1.1) is more verbose:
array = new int[] { 11, 12 , 13, 21, 22 , 21, 31, 32 , 33 };You can put in an extra comma at the end, without ill effect. This makes maintaining vertical lists easier:
array = new int[] { 11, 12, 13, 21, };You can even use anonymous arrays:
Under the hood, to find an element, you first index by row into a contiguous block of pointers. That points you to the start of one of another contiguous block of pointers. You index into that block by column, which points you to associated object. If you had a 3x5 matrix of objects you would have 1+3+(3*5)=19 separate allocation regions for the matrix and its objects.
Here is the generalized way you would use declare and initialize a 3x5 rectangular matrix.
Here is a shorter way:// This is the usual shorthand way to initialise a matrix. int[][] mat = new int[3][5]; for ( int i=0; i<3; i++ ) { for ( int j=0; j<5; j++ ) { mat[i][j] = i*j+100; } }And here is a more realistic way, avoiding hard coded constants:
// Using length instead of hard-coded constants static final int NUM_ROWS = 3; static final int NUM_COLS = 5; int[][] mat = new int [NUM_ROWS][NUM_COLS]; for ( int i=0; i<mat.length; i++ ) { for ( int j=0; j<mat[i].length; j++ ) { mat[i][j] = i*j+100; } }If you fail to initialise the array, Java automatically initialises it for you to zeroes. If you have a matrix of objects, and you fail to initialise, Java initialises it to nulls for you. It does not allocate empty objects at each grid point for you. You have to allocate the objects yourself like this:
Cell[][] mat = new Cell[3][5]; for ( int i=0; i<3; i++ ) { for ( int j=0; j<5; j++ ) { mat[i][j] = new Cell( i, j, 100 ); } }Here is how you could create a triangular matrix: You can initialise a matrix to a list of values this way:
int[][] mat = { { 11 , 12, 13} , { 21, 22 , 21} , { 31 , 32, 33}};You might think you could similarly assign a matrix constant to an array like this:
mat = { { 11, 12, 13} , { 21, 22, 21} , { 31, 32, 33}};However, the syntax needed (introduced with JDK 1.1) is more verbose:
mat = new int [][] { { 11 , 12, 13} , { 21, 22 , 21} , { 31 , 32, 33}};In all these examples, you can use mat.length and mat[i].length to avoid repeating the constants that define the matrix’s dimensions.
For serious matrix work you need a matrix package such as MTJ.
Arrays are like virgins. They are very careful about what they allow in themselves. When you construct an array you declare what sorts of Object you are going to allow into the array. You cannot violate that sacred trust:
For example:
Dog[] mutts = new Dalmatian[20]; mutts[2] = new PitBull(); /* Prang!! */
This will get you an ArrayStoreException since the array, as constructed, only allows Dalmatians (or their subclasses) in it, even though the declaration says it will allow any Dog in.
however this:
Dog[] mutts = new Dog [20]; mutts[2] = new PitBull(); /* is perfectly ok */
Whenever you store an Object into an array, at run time, the JVM checks to make sure the object is of a compatible type. There are a few cases where this check is not necessary, e.g. if Dog had no subclasses.
A common error is to have an Object[] returned from Vector in which all the elements actually are Strings. You can cast the elements individually from Object to String, but you can’t cast the entire array object, Object[] to String[], even if all the individual elements currently happen to be Strings. You could only cast Object[] to String[] if the array Object actually were created as a String[]. To get around this problem, to ensure Vector gives you an array of Strings instead of an array of Objects, use code like this to extract arrays from Vectors:
String[] strArray = ( String[])v.toArray( new String [v.size()] );System.arraycopy will copy element by element casting each as necessary. That allows you to convert an Object[] to a String[]. You can do the same yourself in a for loop. Surprisingly, the for loop technique often generates faster code than array copy. arraycopy will not grow an array to hold the new elements.
You might imagine a cast from Dog[] to Dalmatian[] should be allowed if the array currently contains only Dalamatian elements. It would be an expensive check. Further, you would then have two references to the array. There would be nothing to stop you from later adding some PitBull elements via the Dog[] reference, violating the consistency requirement of the Dalmatian[] reference. That’s why the ArrayStore gotcha has to be there.
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.18] | ![]() | ||
| You are visitor number 99,339. | |||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/array.html | J:\mindprod\jgloss\array.html | ||