ArrayList<String> songs = new ArrayList<String>();
songs.add("Shake It Off");
songs.add("In Da Club");
songs.add("Party In The USA");
songs.add("I Ain't Worried");
songs.add("One Dance");
songs.add("I Wanna Benz");

System.out.println(songs);
[Shake It Off, In Da Club, Party In The USA, I Ain't Worried, One Dance, I Wanna Benz]
// size()
System.out.println("- size()");
System.out.println("There are " + songs.size() + " terms in the ArrayList songs: " + songs);
- size()
There are 6 terms in the ArrayList songs: [Shake It Off, In Da Club, Party In The USA, I Ain't Worried, One Dance, I Wanna Benz]
// Remove(int index or element
songs.remove(3);
System.out.println("- remove(int index)");
System.out.println(songs);
- remove(int index)
[Shake It Off, In Da Club, Party In The USA, One Dance, I Wanna Benz]
// hashCode()
System.out.println("- hashCode()");
System.out.println(songs.hashCode());
- hashCode()
-1832161442
// sort(Comparator comp)
System.out.println("- sort(Comparator comp)");
System.out.println("ArrayList before sort: " + songs);
Collections.sort(songs);
System.out.println("ArrayList after sort: " + songs);
- sort(Comparator comp)
ArrayList before sort: [Shake It Off, In Da Club, Party In The USA, One Dance, I Wanna Benz]
ArrayList after sort: [I Wanna Benz, In Da Club, One Dance, Party In The USA, Shake It Off]
public class AscendingOrder {
    public static void main(String args[]) {
        // creating object of ArrayList class
        ArrayList<Integer> arrlist = new ArrayList<Integer>();
        
        // adding elements to the ArrayList
        arrlist.add(1);
        arrlist.add(3);
        arrlist.add(2);
        
        // printing the unsorted ArrayList
        System.out.println("Before Sorting: "+ arrlist);
        
        // Sorting ArrayList in ascending Order
        Collections.sort(arrlist);
        
        // printing the sorted ArrayList
        System.out.println("After Sorting: "+ arrlist);
    }
}

AscendingOrder.main(null);
Before Sorting: [1, 3, 2]
After Sorting: [1, 2, 3]

Homework (Due Monday 8AM)

  • 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