1 // Fig. 19.2: UsingArrays.
java
2 // Using Java arrays.
3 import [Link];
4
5 public class UsingArrays
6 {
7 private int intArray[] = { 1, 2, 3, 4, 5, 6 };
8 private double doubleArray[] = { 8.4, 9.3, 0.2, 7.9, 3.4 };
9 private int filledIntArray[], intArrayCopy[];
10
11 // constructor initializes arrays
12 public UsingArrays()
13 {
14 filledIntArray = new int [ 10 ]; // create int array with 10
elements
15 intArrayCopy = new int [ [Link] ];
16
17 [Link]( filledIntArray, 7 ); // fill with 7s
18 [Link]( doubleArray ); // sort doubleArray ascending
19
20 // copy array intArray into array intArrayCopy
21 [Link]( intArray, 0, intArrayCopy,
22 0, [Link] );
23 } // end UsingArrays constructor
24
25 // output values in each array
26 public void printArrays()
27 {
28 [Link]( "doubleArray: " );
29 for ( double doubleValue : doubleArray )
30 [Link]( "%.1f ", doubleValue );
31
32 [Link]( "\nintArray: " );
33 for ( int intValue : intArray )
34 [Link]( "%d ", intValue );
35
36 [Link]( "\nfilledIntArray: " );
37 for ( int intValue : filledIntArray )
38 [Link]( "%d ", intValue );
39
40 [Link]( "\nintArrayCopy: " );
41 for ( int intValue : intArrayCopy )
42 [Link]( "%d ", intValue );
43
44 [Link]( "\n" );
45 } // end method printArrays
46
47 // find value in array intArray
48 public int searchForInt( int value )
49 {
50 return [Link]( intArray, value );
51 } // end method searchForInt
52
53 // compare array contents
54 public void printEquality()
55 {
56 boolean b = [Link]( intArray, intArrayCopy );
57 [Link]( "intArray %s intArrayCopy\n",
58 ( b ? "==" : "!=" ) );
59
60 b = [Link]( intArray, filledIntArray );
61 [Link]( "intArray %s filledIntArray\n",
62 ( b ? "==" : "!=" ) );
63 } // end method printEquality
64
65 public static void main( String args[] )
66 {
67 UsingArrays usingArrays = new UsingArrays();
68
69 [Link]();
70 [Link]();
71
72 int location = [Link]( 5 );
73 if ( location >= 0 )
74 [Link](
75 "Found 5 at element %d in intArray\n", location );
76 else
77 [Link]( "5 not found in intArray" );
78
79 location = [Link]( 8763 );
80 if ( location >= 0 )
81 [Link](
82 "Found 8763 at element %d in intArray\n", location );
83 else
84 [Link]( "8763 not found in intArray" );
85 } // end main
86 } // end class UsingArrays