Arrays and Lists
import java.util.ArrayList;
public class WishList {
public static void main(String [] args) {
lists(null);
}
public static void lists(String arg[]) {
// create a ArrayList String type
ArrayList<String> clothesWishlist = new ArrayList<String>();
ArrayList<String> jewelryWishlist= new ArrayList<String>(Arrays.asList("Earrings", "Necklace", "Ring", "Bracelet"));
//adding items to list
clothesWishlist.add("boots");
clothesWishlist.add("dress");
clothesWishlist.add("shirt");
clothesWishlist.add("skirt");
//printing wishlist
System.out.println(""+ clothesWishlist + " : " + clothesWishlist.size() + " amount of wants");
System.out.println("===========");
//checking if list is empty
System.out.println("List doesn't have items? " + clothesWishlist.isEmpty());
System.out.println("===========");
//adding jewlery to clothing list
clothesWishlist.addAll(jewelryWishlist);
System.out.println("" + clothesWishlist);
System.out.println("===========");
//Removing an element
clothesWishlist.remove("boots");
System.out.println(""+ clothesWishlist );
System.out.println("===========");
//get index
System.out.println("Dont want your first item?: " + clothesWishlist.get(0));
System.out.println("===========");
//set index
clothesWishlist.set(0, "jacket");
System.out.println("Your new wishlist: " + clothesWishlist);
System.out.println("===========");
//remove index
clothesWishlist.remove(1);
System.out.println("lets get rid of the second item!");
System.out.println("" + clothesWishlist);
System.out.println("===========");
//indexOf
System.out.println("Where is skirt on the list now?");
System.out.println("Skirt index is: " + clothesWishlist.indexOf("skirt"));
System.out.println("===========");
//lastIndexOf returns -1 because pants were never added
System.out.println("Where is pants last seen on my wishlist? " + clothesWishlist.lastIndexOf("pants"));
System.out.println("===========");
// compares jewelry wishlist and clothing wishlist
System.out.println("are my two wishlists the same? " + clothesWishlist.equals(jewelryWishlist));
System.out.println("===========");
//hashcode
System.out.println(clothesWishlist.hashCode());
System.out.println("===========");
//check if list contains
System.out.println("Are sparkly shoes on my wishlist? " +clothesWishlist.contains("sparkly shoes"));
System.out.println("===========");
//sort by alphabetical order
clothesWishlist.sort(Comparator.naturalOrder());
System.out.println(clothesWishlist);
System.out.println("===========");
//clearing the list
clothesWishlist.clear();
System.out.println(" " + clothesWishlist.size());
}
}
WishList.main(null);