Introduction to Java_chapter6
Introduction to Java_chapter6
SINGLE-DIMENSIONAL ARRAYS
Index 0 1 2 3 4 5 6 7 8 9
Value 85 76 99 38 78 98 89 90 82 88
Array positions (cont’d)
int myFirstArray[ ] = { 0, 0, 0, 0, 0 };
for(double u: myList) {
System.out.println(u);
}
Or
or
public static void ptintArray (int [] array )
X is 1
Y[0] is 5555 2003 Prentice Hall, Inc.
All rights reserved.
Outline
Example for pass by sharing, cont.
•The primitive type value in x is passed to number, and
the reference value in y is passed to numbers.
•For example, the following statement returns a new array list2 with
elements 6,5,4,3,2,1
Int [ ] list1={1,2,3,4,5,6};
Int [ ] list2= reverse(list1); 2003 Prentice Hall, Inc.
All rights reserved.
Outline
Variable-Length Argument Lists
•A variable number of arguments of the same type can be passed to a method and treated as
an array.
•You can pass a variable number of arguments of the same type to a method. The
parameter in the method is declared as follows:
typeName... parameterName
Example:
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Example:
System.arraycopy(sourceArray, 0, targetArray,
0, sourceArray.length);
Outline
The Arrays Class