Array Lists
Working on array lists
// Daniel
import java.util.ArrayList;
public class SongList {
public static void main(String[] args) {
testing(null);
}
public static void testing(String[] args) {
// defining songs as an empty arraylist, insongs is predefined
ArrayList<String> songs = new ArrayList<String>();
ArrayList<String> inSongs = new ArrayList<String>(Arrays.asList("Fancy", "Party in the USA", "Lost in Paris"));
// Add 3 songs to songs
songs.add("House of the Rising Sun");
songs.add("Viva la Vida");
songs.add("Forget You");
// Printing result with size
System.out.println("" + songs + " - " + songs.size() + " items");
System.out.println("----------");
// Adding insongs to songs
songs.addAll(inSongs);
// Printing result
System.out.println("" + songs + " - " + songs.size() + " items");
// Getting a specific variable and then removing it
System.out.println("----------");
System.out.println("This is the element at index 4: " + songs.get(4) + ". Let's remove it.");
songs.remove(4);
System.out.println("" + songs + " - " + songs.size() + " items");
// Removing a specific element based on name
System.out.println("----------");
System.out.println("Removing 'Viva la Vida'");
songs.remove("Viva la Vida");
System.out.println("" + songs + " - " + songs.size() + " items");
// Clearing the entire list
System.out.println("----------");
System.out.println("Clearing the entire list using the clear() function.");
songs.clear();
System.out.println("" + songs + " - " + songs.size() + " items");
}
}
SongList.main(null);
// Everitt
import java.util.Comparator;
public class SongList2 {
public static void main(String[] args) {
testing(null);
}
public static void testing(String[] args) {
ArrayList<String> songs = new ArrayList<String>();
songs.add("House of the Rising Sun");
songs.add("Viva la Vida");
songs.add("Forget You");
System.out.println(songs);
System.out.println("----------");
songs.set(1, "Man");
System.out.println("Replaced Viva La Vida with Man" + songs);
System.out.println("----------");
System.out.println("Index of Man: "+ songs.indexOf("Man"));
System.out.println("----------");
// Should return -1
System.out.println("How many of this song are in the list: " + songs.lastIndexOf("HeeHoo"));
System.out.println("----------");
//Compares entire list
ArrayList<String> songs2 = new ArrayList<String>();
songs2.add("House of the Rising Sun");
songs2.add("Man");
songs2.add("Forget You");
System.out.println(songs.equals(songs2));
System.out.println("----------");
// Hashcode of Songs
System.out.println(songs.hashCode());
System.out.println("----------");
//Checks if the list is empty or not
System.out.println("Is it empty? " + songs.isEmpty());
System.out.println("----------");
//Check if the list contains this certain element or not
System.out.println("Does it have Cheeseburger Joe's Country music in Songs? " + songs.contains("Cheeseburger Joe's Country Music"));
System.out.println("----------");
//Sorts by comparator variable
songs.sort(Comparator.naturalOrder());
System.out.println("Songs are sorted by Alphabet" + songs);
System.out.println("----------");
}
}
SongList2.main(null);