Notes:

  • An ArrayList is a reference type that is mutable (changeable) and contains object references.
  • To initialize an ArrayList, write: ArrayList courses = new ArrayList();</li>
  • To add an element to a particular index in an ArrayList, use the add(int index, element); method. To add all elements in a specified collection to an ArrayList, use the addAll(int index, Collection collection); method.
  • To get the size of an ArrayList, use the size(); method.
  • To remove an element of an ArrayList at a particular index, use the remove(element or int index); method.
  • To retrieve the element at a particular index of an ArrayList, use the get(int index); method.
  • Searching involves locating data within linear structures (e.g. arrays, lists, stacks, etc.). Searching a linear structure can be done with control structures, which include sequential, selection, and iteration.
  • </ul> </div> </div> </div>

    Homework:

    Create an ArrayList that includes 2 of the 4 factors listed below.

    • Sort an ArrayList in descending order and swap the first and last elements
    • Find and display the hashCode of an ArrayList before and after being sorted
    • Return "ascending" if the list is sorted in ascending order, return "descending" if it is descending, and return "neither" if neither
    • Replace 3 elements in an ArrayList with another ArrayList and reverse the order of the new list
    // Import Java Collections library
    import java.util.Collections;
    
    // Class containing ArrayList methods
    public class ArrayLists {
        public ArrayList<Integer> numberList;
        public ArrayList<Integer> numberList2;
        Scanner userInput = new Scanner(System.in);
        // Constructor for initializing ArrayList elements
        public ArrayLists() {
            numberList = new ArrayList<Integer>();
            numberList.add(1);
            numberList.add(10);
            numberList.add(7);
            numberList.add(8);
            numberList2 = new ArrayList<Integer>();
            numberList2.add(3);
            numberList2.add(9);
            numberList2.add(21);
        }
    
        // Method for sorting the ArrayList in descending order and swapping the first and last elements.
        public void descend() {
            ArrayList<Integer> numberListDescend = new ArrayList<Integer>();
            numberListDescend.addAll(numberList);
            Collections.sort(numberListDescend, Collections.reverseOrder());
            int temp = numberListDescend.get(0);
            numberListDescend.set(0, numberListDescend.get(numberListDescend.size() - 1));
            numberListDescend.set(numberListDescend.size() - 1, temp);
            System.out.println("Descending ArrayList with the first and last elements swapped:");
            for (int item : numberListDescend) {
                System.out.print(item + " ");
            }
            System.out.println("");
            getHashCode(numberListDescend);
        }
    
        // Method that gets the hash code of the ArrayList before and after it is sorted in descending order
        public void getHashCode(ArrayList<Integer> numberListDescend) {
            System.out.println("Hash code of ArrayList before sorting: " + numberList.hashCode());
            System.out.println("Hash code of ArrayList after sorting: " + numberListDescend.hashCode());
        }
    
        // Check if given ArrayList is in ascending order
        public boolean isAscending() {
            for (int i = 0; i < numberList.size() - 1; i++) {
                if (numberList.get(i) > numberList.get(i + 1)) {
                    return false;
                }
            }
            return true;
        }
    
        // Check if given ArrayList is in descending order
        public boolean isDescending() {
            for (int i = 0; i < numberList.size() - 1; i++) {
                if (numberList.get(i) < numberList.get(i + 1)) {
                    return false;
                }
            }
            return true;
        }
    
        // Method for identifying ArrayList as either ascending, descending, or neither
        public void sortCheck() {
            if (isAscending() == true) {
                System.out.println("ArrayList numberList is ascending");
            } else if (isDescending() == true) {
                System.out.println("ArrayList numberList is descending");
            } else {
                System.out.println("ArrayList numberList is neither ascending nor descending");
            }
        }
    
        // Method that replaces 3 elements of an ArrayList with elements of another ArrayList, then reverses the former ArrayList
        public void replacement() {
            ArrayList<Integer> numberListReplacement = new ArrayList<Integer>();
            numberListReplacement.addAll(numberList);
            numberListReplacement.set(0, numberList2.get(0));
            numberListReplacement.set(1, numberList2.get(1));
            numberListReplacement.set(2, numberList2.get(2));
            Collections.reverse(numberListReplacement);
            System.out.println("ArrayList with 3 values replaced and is reversed:");
            for (int item : numberListReplacement) {
                System.out.print(item + " ");
            }
            System.out.println("");
        }
    }
    
    // Main class for running code
    class Main {
        // Tester function for visualizing outputs
        public static void main(String[] args) {
            ArrayLists myObj = new ArrayLists();
            System.out.println("Original ArrayList:");
            for (int item : myObj.numberList) {
                System.out.print(item + " ");
            }
            System.out.println("");
            myObj.descend();
            myObj.sortCheck();
            myObj.replacement();
        }
    }
    
    Main.main(null);
    
    Original ArrayList:
    1 10 7 8 
    Descending ArrayList with the first and last elements swapped:
    1 8 7 10 
    Hash code of ArrayList before sorting: 963147
    Hash code of ArrayList after sorting: 961227
    ArrayList numberList is neither ascending nor descending
    ArrayList with 3 values replaced and is reversed:
    8 21 9 3 
    
    </div>