0% found this document useful (0 votes)
20 views

Import Public Class Private Int Private Double Private Int

This Java program defines a UsingArrays class that uses arrays to store and manipulate integer and double values. The constructor initializes several arrays, including filling one with 7s and sorting another. Methods print the array values, compare two arrays for equality, and search an array for a given integer value using binary search. The main method tests the class by printing arrays, comparing them, and searching for values.

Uploaded by

Kristin Fisher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Import Public Class Private Int Private Double Private Int

This Java program defines a UsingArrays class that uses arrays to store and manipulate integer and double values. The constructor initializes several arrays, including filling one with 7s and sorting another. Methods print the array values, compare two arrays for equality, and search an array for a given integer value using binary search. The main method tests the class by printing arrays, comparing them, and searching for values.

Uploaded by

Kristin Fisher
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 // Fig. 19.2: UsingArrays.

java
2 // Using Java arrays.
3 import java.util.Arrays;
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 [ intArray.length ];
16
17 Arrays.fill( filledIntArray, 7 ); // fill with 7s
18 Arrays.sort( doubleArray ); // sort doubleArray ascending
19
20 // copy array intArray into array intArrayCopy
21 System.arraycopy( intArray, 0, intArrayCopy,
22 0, intArray.length );
23 } // end UsingArrays constructor
24
25 // output values in each array
26 public void printArrays()
27 {
28 System.out.print( "doubleArray: " );
29 for ( double doubleValue : doubleArray )
30 System.out.printf( "%.1f ", doubleValue );
31
32 System.out.print( "\nintArray: " );
33 for ( int intValue : intArray )
34 System.out.printf( "%d ", intValue );
35
36 System.out.print( "\nfilledIntArray: " );
37 for ( int intValue : filledIntArray )
38 System.out.printf( "%d ", intValue );
39
40 System.out.print( "\nintArrayCopy: " );
41 for ( int intValue : intArrayCopy )
42 System.out.printf( "%d ", intValue );
43
44 System.out.println( "\n" );
45 } // end method printArrays
46
47 // find value in array intArray
48 public int searchForInt( int value )
49 {
50 return Arrays.binarySearch( intArray, value );
51 } // end method searchForInt
52
53 // compare array contents
54 public void printEquality()
55 {
56 boolean b = Arrays.equals( intArray, intArrayCopy );
57 System.out.printf( "intArray %s intArrayCopy\n",
58 ( b ? "==" : "!=" ) );
59
60 b = Arrays.equals( intArray, filledIntArray );
61 System.out.printf( "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 usingArrays.printArrays();
70 usingArrays.printEquality();
71
72 int location = usingArrays.searchForInt( 5 );
73 if ( location >= 0 )
74 System.out.printf(
75 "Found 5 at element %d in intArray\n", location );
76 else
77 System.out.println( "5 not found in intArray" );
78
79 location = usingArrays.searchForInt( 8763 );
80 if ( location >= 0 )
81 System.out.printf(
82 "Found 8763 at element %d in intArray\n", location );
83 else
84 System.out.println( "8763 not found in intArray" );
85 } // end main
86 } // end class UsingArrays

You might also like