char[ ] letterGrade = { ’ A ’ , ‘ B ’ , ‘C’, ‘ D ’ , ‘ F ’ };
It is actually the compiler that fills the gap. Thus, in the first example, the compiler would
add the following:
int[] prime = new int[10];
prime[0] = 2; prime[1] = 3; ... prime[9] = 29;
Observe that when an initializer list is used:
The new operator is not required.
The size is not required; it is computed by the compiler.
Copying one array to another
Consider the following declarations:
int[ ] array1 = {22, 3, 50, 7, 11, 13, 17};
int[ ] array2 = {3, 5, 8, 20, 0, 100, 40};
The assignment statement :
array1 = array2;
does not copy the contents of array2 to array1 ; it makes the reference array1 to refer to the array object referenced by array2 . The object that was referenced by array1 becomes garbage.
To copy the contents of array2 to array1 , code like the following is used:
for(int k = 0; k < array2.length; k++)
array1[k] = array2[k];
Array used as a Parameter
Remember that a parameter is data that is supplied to a method just before it starts running.
The following method prints the content of an int array passed to it as parameter:
class MyArray {
public void print (int[] x) {
for(int i = 0; i < x.length; i++)
System.out.print(x[i]+ " ");
System.out.println();
}
}
The method is written using the parameter x to refer to the actual data that it will be supplied with.
The method is written without referring to any particular data. When the print() method is running it has a reference to an array object in its parameter x .
Example(1)
class ArrayDemo {
public static void main ( String[] args ) {
MyArray operate = new MyArray();
int[] ar1 = { -20, 19, 1, 5, -1, 27 } ;
System.out.print ("The array is: " );
operate.print( ar1 );
}
}
public static void print (int[] x) { for(int i = 0; i < x.length; i++) System.out.print(a[x]+ " "); System.out.println(); } MyArray print(x) operate -20 19 1 5 -1 27 ar1
Parameter Connected to New Data
Could the main() method create a second array and use the print() method with it?
Example (2): What is the output?
class ArrayDemo {
public static void main ( String[] args ) {
MyArray operate = new MyArray();
int[] ar1 = { -20, 19, 1, 5, -1, 27 } ;
int[] ar2 = {1, 2, 6, 3, 9, 5};
System.out.print("The array is: " );
operate.print( ar1 );
System.out.print("The second array is: " );
operate.print( ar2 );
}
}
The array is: -20 19 1 5 -1 27 The second array is: 1 2 6 3 9 5
Using Two-Dimensional and Multidimensional Arrays
One-dimensional or single-dimensional array
Picture as column of values
Elements accessed using single subscript
Two-dimensional arrays
Two or more columns of values
Rows and columns
Use two subscripts
Matrix or table
int[][] someNumbers = new int[3][4];
View of a Two-Dimensional Array in Memory
Using Two-Dimensional and Multidimensional Arrays (continued)
int[][] rents = { {400, 450, 510},
{500, 560, 630},
{625, 676, 740},
{1000, 1250, 1600} };
public static void displayScores(int[][]scoresArray)
Using Two-Dimensional and Multidimensional Arrays (continued)
Multidimensional arrays
More than two dimensions
Create arrays of any size
Keep track of order of variables needed as subscripts
0 comments
Post a comment