package CollectionEx; // quick and dirty without typing java.util.ArrayList etc import java.util.*; // example of using List classes public class ListExample { // assumes we have a Cat class public static void catExample() { List cattery = new ArrayList<>(); cattery.add(new Cat("Muffy", 2)); cattery.add(new Cat("Fluffy", 5)); cattery.add(new Cat("Clyde", 3)); for (Cat elt : cattery ) { elt.miaow(); } // create a cat we can use for searching for Fluffy Cat searchCat = new Cat("Fluffy", 5); // based on Cat's equals, see if cat is there if (cattery.contains(searchCat)) { // index where cat is found int foundIndex = cattery.indexOf(searchCat); // cat in List at that index Cat foundCat = cattery.get(foundIndex); // update the cat inside the array foundCat.setAge(foundCat.getAge() +1); // same thing no temp variables cattery.get(cattery.indexOf(searchCat)).setAge( cattery.get(cattery.indexOf(searchCat)).getAge() +1); } } // example with strings public static void stringExample() { // ArrayList is a generic type, here we make one of strings //ArrayList monsters = new ArrayList(); //ArrayList monsters = new ArrayList<>(); // empty diamond -- type inference List monsters = new ArrayList<>(); // polymorphism, could swap in LinkedList //List monsters = new LinkedList<>(); // polymorphism, could swap in LinkedList // can check for empty list if (monsters.isEmpty()){ System.out.println("Yes of course it's empty"); } // putting new items in list monsters.add("Dalek"); monsters.add("Cyberman"); monsters.add("Voords"); monsters.add("Nimon"); monsters.add("Silurians"); monsters.add("Autons"); monsters.add("Axons"); // ArrayList toString is friendly System.out.println("Original list " + monsters); // also works with for each for(String s : monsters) { System.out.println(s); } // find how many items .size() not .length System.out.println("list contains " + monsters.size() + " items."); // check for items .contains( System.out.println("list contains Nimon: " + monsters.contains("Nimon")); System.out.println("list contains Slitheen: " + monsters.contains("Slitheen")); // find the index of an item .indexOf( System.out.println("Autons are at " + monsters.indexOf("Autons")); // get item at a certain index .get( String astring = monsters.get(3); System.out.println("Found at index 3: " + astring); // remove an item from the list -- leaves no hole .remove( // returns a boolean so we know if it was there in the first place boolean voordsWereThere = monsters.remove("Voords"); // true if (voordsWereThere) { System.out.println("without Voords: " + monsters); } boolean vogonsWereThere = monsters.remove("Vogons"); // false if (vogonsWereThere) { System.out.println("without Vogons " + monsters); } // can also remove based on index -- here removing the last item monsters.remove(monsters.size()-1); System.out.println("shorter list " +monsters); // insert an item at a certain index -- moves rest of items down monsters.add(3, "Vardans"); System.out.println("after insert " + monsters); // set the item at a certain index to a new value, replacing the old monsters.set(4, "Aboriginal Earth Reptiles"); System.out.println("after set " + monsters); // ArrayList.RangeCheck exception -- there are only 6 spots in the list, can't put an item at index 8 //monsters.set(8, "Haemovores"); monsters.add("Drashigs"); monsters.add("Tractators"); monsters.add("Tereleptils"); monsters.add("Movellans"); monsters.add("Draconians"); monsters.add("Sontarans"); System.out.println("more stuff: " + monsters); //sort the list -- CHANGES CONTENTS OF LIST!! Collections.sort(monsters); // uses compareTo from String System.out.println("alphabetical: " + monsters); // ITERATORS // iterator helps get at the elements of an ArrayList ListIterator lit = monsters.listIterator(); // as long as there is one more element after current position, print it with its index // hasNext checks if there is one // next gets the next one and updates the index while(lit.hasNext()) { System.out.println(lit.nextIndex() + ": " + lit.next()); } // now we must be past the end of the list // now at end of the list, but we can move backwards while(lit.hasPrevious()) { System.out.println(lit.previousIndex() + ": " + lit.previous()); } // passing in monsters.size() to start iterator at end of list ListIterator backlit = monsters.listIterator(monsters.size()); // as long as there is one more element before current position, print it with its index while(backlit.hasPrevious()) { System.out.println(backlit.previousIndex() + ": " + backlit.previous()); } // new arraylist to hold the ones with long names List longMonsters = new ArrayList<>(); // remember lit is back at start again while (lit.hasNext()){ // remove all the ones that are too long String curr = lit.next(); if (curr.length() > 8) { // put into the other arraylist longMonsters.add(curr); // removes whatever was most recently returned from next() lit.remove(); } } System.out.println("List with long names removed:"); // foreach works, has no moving backward, no remove for (String s : monsters) { System.out.println(s); } System.out.println("Just the long monsters"); for (String s : longMonsters) { System.out.println(s); } } // call examples public static void main(String[] args) { stringExample(); } }