Using Arrays

Even if your array is filled with primitive ints, every array in Java is an object, and is treated as such. Arrays are objects. So there are a number of methods that you can put to work to get value out of them. It also means that you should comply with object assignment and equivalency tests, as well as state and local laws.

You can think of two-dimensional arrays as holding a row and column number, in that order. For example, take Table 14-1.

14-1. A Two Dimensional Array
 012
07846
1927892
2988166
3715689
4554345

This table represents a two-dimensional array that holds 5 rows and 3 columns. You would create such an array like this:

int[][] grades = new int[5][3];

The element at position [3][2] is 89. The element at position [0][0] is 78. The element at position [4][2] is 45. The element at [0][1] is empty. The element at position [4][3] doesn't exist, and trying to access it gets you an ArrayIndexOutOfBoundsException.

grades[0][1] = 90 ;
// puts 90 into row 0, column 1.
grades[2][ 2]++ ;
//increments value at row 2, column 2 by 1

Copying Array Elements

If you want to do this, you can simply assign a new array to the old array, and then you have your data in the new array, right? Well, yes, but this is shared data. If somebody updates that data, both arrays get the update, and this might not be desirable. So the API designers were thoughtful enough to allow us to do this via the aforementioned System.arraycopy() method.

import java.lang.Math.*;
//...
System.arraycopy(sourceArray, 0, destArray, 0,
        min(sourceArray.length, destArray.length));

Remember that length is a field of the Array class; it isn't length() or getLength(). The preceding code uses the static import feature new to Java 5.0, so it won't compile on earlier versions. But that's easily fixed.

The following class shows how to use two-dimensional arrays and how to use the System.arraycopy() method.

UsingArrays.java
package net.javagarage.arrays;

/**<p>
 * Shows how to use arrays after they are created.
 * </p>
 * @author eben hewitt
 * @see
 **/
   //import all static methods of the Math class as
   //convenience
import static java.lang.Math . *;

public class UsingArrays {

public static void main(String[] args) {
//create an instance (object) of this class
UsingArrays demo = new UsingArrays();

//now call the methods defined in this class
//on the object reference
demo.useSquareArray();
demo.copy();

}

/**
 *Creates an array of 100 elements (10 * 10 = 100),
 *and populates them with values.
 */
private void useSquareArray(){
int[][] square = new int[10][10];

//loop over to populate.

for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
square[i][j] = j+1;
System.out.print(square[i][j]);
}
System.out.println();
}
}


private void copy(){
char[] sourceArray = {'s','e','c','r','e','t'};
char[] destArray = new char[6];
//the arraycopy method copies the contents and not
//merely the references
System.arraycopy(sourceArray, 0, destArray, 0,
      min(sourceArray.length, destArray.length));

System.out.println(destArray[2]);
}


}

The result is as follows:

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
c

Shifting Array Elements

You can shift all of the elements in an array over to the right or the left with a little simple code. This uses the arraycopy() method, which we just saw.

//say you've got an array called array
//shift all elements right by one
System.arraycopy(array, 0, array, 1, array.length-1);

//shift all elements left by one
System.arraycopy(array, 1, array, 0, array.length-1);

The arraycopy() method (which should have been called arrayCopy to follow Sun's own naming conventions) takes the following parameters:

srcThe source array
srcPosStarting position in the source array
destinationThe destination array
destStartPosStarting position in the destination data
lengthThe number of array elements to be copied

Note that you have to know what you're doing when you use this method. It throws NullPointerException, IndexOutOfBounds-Exception, or ArrayStoreException, if the source or destination arrays are null, or if the system can't squeeze your big, honkin' data into that little array. Whew.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.81.240