Notes

  • A reference type
  • Change size dynamically
  • methods:
    • add(int index, element)
    • size()
    • clear()
    • remove()
    • get(int index)
  • Enhanced for loops
    • iterate through every element in a list
  • Searching - locating data within linear structures (arrays, lists, queue, stack, etc.)
  • For loops are very important for linear searching because it needs to be specified sequentially
  • No need to track index after execution
  • Order matters in seaching
    • 5 rubber ducks with an individual number (green, pink, pink, blue, green)
    • Removing 1 pink duck - move through each duck to see if its pink
  • Sort in ascending and descending order

Homework

  • Sophie and I pair-coded this 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
public class ArrayListHomework
{
    private static ArrayList <Integer> list = new ArrayList <Integer>();
    private static ArrayList <Character> letters = new ArrayList <Character>();

    // prints letters before the swap and sort
    public void getLetters()
    {
        letters.add(0,'X');
        letters.add(1,'A');
        letters.add(2,'F');
        letters.add(3,'O');

        System.out.println(letters);
    }

    // sorts letters in descending order and prints them
    public void sortDescending ()
    {
        Collections.sort(letters, Collections.reverseOrder());
        System.out.println(letters);
    }

    // swaps first and last elements of letters
    public void swap()
    {
        char temp = letters.get(0);
        letters.set(0, letters.get(letters.size()-1));

        letters.set(letters.size()-1, temp);
        System.out.println(letters);
    }

    // prints before the numbers list is sorted into ascendign order and after
    public void beforeAndAfterSort()
    {
        list.add(0,4);
        list.add(1,37);
        list.add(2,2);
        list.add(3,-5);

        System.out.printf("Before sort:");
        System.out.println(list);

        Collections.sort(list);
        
        System.out.printf("After sort:");
        System.out.println(list);
        System.out.println(checkSort(list));
    }

    public String checkSort(ArrayList <Integer> array)
    {
        int ascending = 0;
        int descending = 0;
        for(int i = 0; i < array.size()-1; i++)
        {
            if(array.get(i) < array.get(i+1))
            {
                ascending++;
            }
            else
            {
                descending++;
            }
        }

        if(ascending >= array.size()-1)
        {
            return "Ascending";
        }
        if (descending >= array.size()-1)
        {
            return "Descending";
        }
        return "Error";
    }

    // main/tester method
    public static void main (String[] Args)
    {
        ArrayListHomework arrayListHomework = new ArrayListHomework();

        arrayListHomework.beforeAndAfterSort();
        System.out.printf("\n");

        System.out.printf("Before sort:");
        arrayListHomework.getLetters();
        System.out.printf("Descending order sort:");
        arrayListHomework.sortDescending();
        System.out.printf("First and last elements swapped:");
        arrayListHomework.swap();
    }
}
ArrayListHomework.main(null);
Before sort:[4, 37, 2, -5]
After sort:[-5, 2, 4, 37]
Ascending

Before sort:[X, A, F, O]
Descending order sort:[X, O, F, A]
First and last elements swapped:[A, O, F, X]