import java.util.ArrayList; public class showArrayList { ArrayList alint; ArrayList adoub; ArrayList astring; ArrayList aloc; public class Location { int x; int y; public Location(int x, int y) { this.x = x; this.y = y; } public boolean equals (Object o) { Location p = (Location) o; if ((x == p.x) && (y == p.y)) return true; else return false; } public String toString () { return "(" + x + "," + y + ")"; } } private void showDeclarations() { // ArrayList ar = new ArrayList(); //uncomment this for error ArrayList ar = new ArrayList(); // ArrayList ar = new ArrayList(); // Not a hard error, but not recommended this.alint = ar; // // Now for the rest this.adoub = new ArrayList(); this.astring = new ArrayList(); this.aloc = new ArrayList(); return; } private void showDoubleMethods() { this.adoub.add(1.0); this.adoub.add(-1.0); System.out.println(this.adoub); this.adoub.add(1,0.0); System.out.println(this.adoub); this.adoub.remove(0); System.out.println(this.adoub); if (!this.adoub.contains(new Double(2.0))) System.out.println ("2.0 not found in array"); } private void showPointMethods() { Location l; //Show adding then clearing System.out.println ("Show add, get, indexOf, then clear"); this.aloc.add(new Location(1,2)); System.out.println (aloc); l = this.aloc.get(0); System.out.println ("get at index 0 = " + l); int index = this.aloc.indexOf(l); System.out.println ("The index of the Location " + l + "is " + index); this.aloc.clear(); if (this.aloc.isEmpty()) System.out.println ("aloc is empty"); System.out.println(aloc); index = this.aloc.indexOf(l); System.out.println ("The index of the Location " + l + "is " + index); //show contains Location l1 = new Location(0,0); Location l2 = new Location(0,0); System.out.println("\nShow equality/contains"); if (l2 != l1) System.out.println ( "l1 != p2"); if (l2.equals(l1)) System.out.println ("l1 .equals l2"); this.aloc.add(l2); System.out.println(aloc); if (aloc.contains(l1)) System.out.println ("Contains check for loc = true"); else System.out.println ("Contains check for loc = false"); //lastIndexOf System.out.println ("\nShow lastIndexof"); aloc.add(new Location(1,1)); aloc.add(new Location(0,1)); aloc.add(new Location(1,1)); aloc.add(new Location(0,1)); index = aloc.lastIndexOf(new Location(1,1)); System.out.println ("The last index of (1,1) is " + index); System.out.println ("The size of aloc = " + aloc.size()); //Remove System.out.println(aloc); aloc.remove(l1); System.out.println(aloc); aloc.remove(new Location(0,1)); System.out.println(aloc); } private void showStrings() { astring.add("Yessir"); if (astring.contains("Yessir")) System.out.println ("Contains check for Yessir = true"); else System.out.println ("Contains check for Yessir = false"); // String s2a = "No"; String s2b = "sir"; // String s2 = s2a + s2b; String s2 = "No" + s2b; astring.add(s2); String s1 = "Nosir"; if (s2 != s1) System.out.println ("Strings are not equal"); if (astring.contains(s1)) System.out.println ("Contains check for Nosir = true"); else System.out.println ("Contains check for Nosir = false"); } public static void main(String[] args) { showArrayList s = new showArrayList(); s.showDeclarations(); System.out.println(); s.showDoubleMethods(); System.out.println(); s.showPointMethods(); System.out.println(); s.showStrings(); } }