Arrays
The Arrays class
Code
Code that uses the fill method
int[] quantities = new int[5]; Arrays.fill(quantities, 1); // all elements are set to 1 Code that uses the fill method to fill 3 elements in an array int[] quantities = new int[5]; Arrays.fill(quantities, 1, 4, 100); // elements 1, 2, and 3 are
Code that uses the equals method
String[] titles1 = {"War and Peace", "Gone With the Wind"};
String[] titles2 = {"War and Peace", "Gone With the Wind"};
if (titles1 == titles2)
System.out.println("titles1 == titles2 is true");
else
System.out.println("titles1 == titles2 is false");
if (Arrays.equals(titles1, titles2))
System.out.println("Arrays.equals(titles1, titles2) is true");
else
System.out.println("Arrays.equals(titles1, titles2) is false");
The console output
titles1 == titles2 is false Arrays.equals(titles1, titles2) is true
Code that uses the sort method
int[] numbers = {2,6,4,1,8,5,9,3,7,0};
Arrays.sort(numbers);
for (int num : numbers)
{
System.out.print(num + " ");
}
The console output
0 1 2 3 4 5 6 7 8 9
Code that uses the sort and binarySearch methods
String[] productCodes = {"mcbl", "jsps", "java"};
Arrays.sort(productCodes);
int index = Arrays.binarySearch(productCodes, "mcbl"); // sets index to 2