package javareview; import java.util.ArrayList; /** * * @author Mayelin */ public class JavaReview3 { public static void main(String[] args) { /****************************/ /* Chapter 8 */ /****************************/ // declare an array of int values int[] array1; array1 = new int[4]; // initialize the elements array1[0] = 100; array1[1] = 200; array1[2] = 300; array1[3] = 400; // create a second array using an initialization list int[] array2 = {3, 5, 1, 9}; // access the elements in array_1 System.out.println("Element at index 0: " + array1[0]); System.out.println("Element at index 1: " + array1[1]); System.out.println("Element at index 2: " + array1[2]); System.out.println("Element at index 3: " + array1[3]); // Example of two variables referencing the same array array2 = array1; // Change one of the elements using array1. array1[0] = 200; // Change one of the elements using array2. array2[3] = 1000; // Display all the elements using array1 System.out.println("The contents of array1:"); for (int value : array1) System.out.print(value + " "); System.out.println(); // Display all the elements using array2 System.out.println("The contents of array2:"); for (int value : array2) System.out.print(value + " "); System.out.println(); // create a copy of array1 - Could also use the System.arraycopy method array2 = createCopyOfArray( array1 ); // make changes to array2 array2[0] = 8; array2[2] = 7; // Display all the elements in array1 System.out.println("The contents of array1:"); for (int value : array1) System.out.print(value + " "); System.out.println(); // Display all the elements in array2 System.out.println("The contents of array2:"); for (int value : array2) System.out.print(value + " "); System.out.println(); System.out.println("The total of the values stored in array1 is: " + getTotalValue(array1) + "\n"); System.out.println("The highest value stored in array2 is: " + getHighestValue(array2) + "\n"); testArrayOfStudents(); testTwoDimensionalArray(); int[][] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12} }; rowSumOf2DArray( numbers ); testArrayLists(); } /** The createCopyOfArray method creates a copy of the array that is passed as an argument. @param array The array to be copied. @return The new array. */ public static int[] createCopyOfArray(int[] array) { int[] newArray = new int[array.length]; for( int i = 0; i < array.length; i++) newArray[i] = array[i]; return newArray; } /** The getTotalValue method returns the sum of all values stored in the given array. @param array The array @return The sum of all values stored in the array that is passed as an argument. */ public static double getTotalValue(int[] array) { double total = 0.0; // Accumulator // Accumulate the sum of the elements in the array. for (int index = 0; index < array.length; index++) total += array[index]; // Return the total. return total; } /** The getHighestValue method returns the highest value stored in the given array. @param array The array @return The highest value stored in the array that is passed as an argument. */ public static int getHighestValue(int[] array) { int highest = array[0]; for (int index = 1; index < array.length; index++) { if (array[index] > highest) highest = array[index]; } return highest; } /** The testArrayOfStudents method creates an array of Student objects and iterates through the elements. */ public static void testArrayOfStudents() { // Array of Student objects Student[] studentList = { new Student("Michael"), new Student("Mary"), new Student("John") }; System.out.println("\n*****************************************"); for (int i = 0; i < studentList.length; i++) { System.out.println("Student # " + (i + 1)); System.out.println("Name: " + studentList[i].getName()); System.out.println("*****************************************"); } } /** The testTwoDimensionalArray method creates a 2 dimensional array. It then displays some of the values. */ public static void testTwoDimensionalArray() { String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"} }; // Mr. Smith System.out.println("\n" + names[0][0] + names[1][0]); // Ms. Jones System.out.println(names[0][2] + names[1][1] ); } /** The rowSumOf2DArray method displays the sum of the values stored in each inner array. @param array The 2 dimensional array whose inner arrays are to be added up. */ public static void rowSumOf2DArray(int[][] array) { int rowTotal; for( int i=0; i < array.length; i++ ) { rowTotal = 0; for( int j=0; j < array[i].length; j++ ) rowTotal += array[i][j]; System.out.println("Total of row " + i + " is " + rowTotal ); } } /** The testArrayLists method creates ArrayList objects and calls some of their methods. */ public static void testArrayLists() { System.out.println("\nTesting an array list of Strings"); ArrayList listOfNames = new ArrayList<>(); System.out.println("# of elements " + listOfNames.size()); listOfNames.add("John"); listOfNames.add(0, "Mary"); listOfNames.set(1, "Juan"); listOfNames.add("Peter"); System.out.println("# of elements " + listOfNames.size()); listOfNames.remove(1); System.out.println(listOfNames); for( String element : listOfNames) System.out.println(element); for (int i = 0; i < listOfNames.size(); i++) { System.out.println("Element at position " + i + " is: " + listOfNames.get(i)); } System.out.println("\nTesting an array list of Integers"); // create an ArrayList of Integers ArrayList listOfNumbers = new ArrayList<>(); System.out.println( "The size of the array list before adding elements is " + listOfNumbers.size() + "."); Integer element1 = new Integer(23); Integer element2 = 20; // java automatically converts an int into an // Integer (autoboxing) listOfNumbers.add(element1); listOfNumbers.add(element2); listOfNumbers.add(24); // autoboxing is happening here as well listOfNumbers.add(21); System.out.println(listOfNumbers); // look for the element that has the value of age Integer age = 24; boolean foundFlag = false; int i = 0; while( i < listOfNumbers.size() && !foundFlag) { if (listOfNumbers.get(i).compareTo(age) == 0) { System.out.println("The value " + age + " was found at position " + i + "."); foundFlag = true; } i++; } if (!foundFlag) System.out.println("The value " + age + " was not found in the list."); } }