Initializing an ArrayList

ArrayList<Type> someName = new ArrayList<Type>();

Adding to an ArrayList

someName.add(someValue);

Removing from an ArrayList

someName.remove(someValue);

Getting the size of an ArrayList

someName.size();

Sorting

  • Collections.sort(someName);
  • Collections.sort(someName, Collections.naturalOrder());

Searching

  • Can use iteration and selection to search
  • For loop, for each loop, while loop

Homework Hacks

  • 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.util.*;

public class ArraylistHacks {
    public ArrayList<Integer> initial_list;
    public ArrayList<Integer> sortswapped_list;
    public String ascdesc = "Neither";

    public ArraylistHacks(ArrayList<Integer> x) {
        // Clone arraylist to avoid overriding values of initial
        this.initial_list = (ArrayList<Integer>) x.clone();
        this.sortswapped_list = (ArrayList<Integer>) x.clone();

        ascDescTest();
        sortAndSwap();
    }

    public void sortAndSwap() {
        // sorting the arraylist
        Collections.sort(sortswapped_list);
        Collections.swap(sortswapped_list, 0, sortswapped_list.size() - 1);
        
    }

    public void ascDescTest() {
        boolean asc = true;
        boolean desc = true;

        for (int i = 0; i < initial_list.size() - 1; i++) {
            // Test if ascending
            if (!(initial_list.get(i) <= initial_list.get(i + 1))) {
                asc = false;
            }
            // Test if descending
            if (!(initial_list.get(i) >= initial_list.get(i + 1))) {
                desc = false;
            }
        }

        // if asc or desc booleans are true, change string output
        if (asc) {
            this.ascdesc = "Ascending";
        }
        else if (desc) {
            this.ascdesc = "Descending";
        }


    }

    public void hacksToString() {
        System.out.println("-----");
        System.out.println("Initial: " + this.initial_list);
        System.out.println("Ascending/Descending: " + this.ascdesc);
        System.out.println("Sort + Swap: " + this.sortswapped_list);
    }

}

ArrayList<Integer> alist = new ArrayList<Integer>();
alist.add(3);
alist.add(2);
alist.add(5);
alist.add(7);
alist.add(12);

ArrayList<Integer> blist = new ArrayList<Integer>();
blist.add(1);
blist.add(2);
blist.add(4);
blist.add(6);
blist.add(8);

ArrayList<Integer> clist = new ArrayList<Integer>();
clist.add(9);
clist.add(8);
clist.add(7);
clist.add(6);
clist.add(5);

ArraylistHacks a = new ArraylistHacks(alist);
a.hacksToString();
ArraylistHacks b = new ArraylistHacks(blist);
b.hacksToString();
ArraylistHacks c = new ArraylistHacks(clist);
c.hacksToString();
-----
Initial: [3, 2, 5, 7, 12]
Ascending/Descending: Neither
Sort + Swap: [12, 3, 5, 7, 2]
-----
Initial: [1, 2, 4, 6, 8]
Ascending/Descending: Ascending
Sort + Swap: [8, 2, 4, 6, 1]
-----
Initial: [9, 8, 7, 6, 5]
Ascending/Descending: Descending
Sort + Swap: [9, 6, 7, 8, 5]