ArrayLists - AP Test Prep
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);
// size()
System.out.println("- size()");
System.out.println("There are " + songs.size() + " terms in the ArrayList songs: " + songs);
// Remove(int index or element
songs.remove(3);
System.out.println("- remove(int index)");
System.out.println(songs);
// hashCode()
System.out.println("- hashCode()");
System.out.println(songs.hashCode());
// 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);
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);
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