Source of ListBasics.java


  1: import java.util.List;
  2: import java.util.ArrayList;
  3: import java.util.LinkedList;
  4: import java.util.Scanner;

  6: /**
  7:  * Create and use some ArrayList objects (and maybe a LinkedList, too)
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class ListBasics {

 13:     /**
 14:      * @param args the command line arguments
 15:      */
 16:     public static void main(String[] args) {
 17:         // create some list objects
 18:         List<String> myWords = new LinkedList<>();
 19:         List<Student> myClass = new ArrayList<>();
 20:         List<Integer> myInts = new ArrayList<>();
 21:         List<Double> myDbls = new ArrayList<>();
 22:         List<Double> myOtherDbls = new LinkedList<>();

 24:         // add some values to them
 25:         myWords.add("ten");
 26:         myWords.add("twenty");
 27:         myWords.add("thirty");
 28:         myWords.add("forty");
 29:         myWords.add("25");
 30:         myClass.add(new Student("Bob"));
 31:         myClass.add(new Student("Carol"));
 32:         myInts.add(10);
 33:         myInts.add(20);
 34:         myInts.add(30);
 35:         myDbls.add(10.0);
 36:         myDbls.add(20.0);
 37:         myDbls.add(1, 15.0);        // add at location 1 (2nd position)
 38:         myOtherDbls.add(10.0);
 39:         myOtherDbls.add(20.0);
 40:         myOtherDbls.add(0, 15.0);   // add at location 0 (1st position)

 42:         // CANNOT add items of the wrong type
 43:         // myWords.add(100);
 44:         // myClass.add("Ted");
 45:         // myInts.add(10.5);
 46:         // myDbls.add("thirty");
 47:         // myOtherDbls.add(new Student("Alice"));

 49:         // print them out
 50:         System.out.println("Here are some lists I've created:");
 51:         System.out.println("\tmyWords == " + myWords);
 52:         System.out.println("\tmyClass == " + myClass);
 53:         System.out.println("\tmyInts == " + myInts);
 54:         System.out.println("\tmyDbls == " + myDbls);
 55:         System.out.println("\tmyOtherDbls == " + myOtherDbls);
 56:         pause();
 57:         
 58:         // look at some of the elements
 59:         System.out.println("Let's get some values from them:");
 60:         String word0 = myWords.get(0);
 61:         System.out.println("\tThe 1st word in " + myWords
 62:                 + " is " + word0);
 63:         System.out.println("\tThe 3rd word in " + myWords
 64:                 + " is " + myWords.get(3 - 1));
 65:         pause();

 67:         // how long is that list?
 68:         System.out.println("What size are these lists?");
 69:         System.out.println("\tThere are " + myWords.size()
 70:                 + " elements in " + myWords);
 71:         System.out.println("\tThere are " + myInts.size()
 72:                 + " elements in " + myInts);
 73:         pause();

 75:         // add some values (not on the end)
 76:         System.out.println("Adding 15 at position 1 of " + myWords);
 77:         myWords.add(1, "fifteen");
 78:         System.out.println("\tmyWords == " + myWords);
 79:         System.out.println("\tThe 3rd word in " + myWords
 80:                 + " is now " + myWords.get(3 - 1));
 81:         pause();

 83:         // check if it has certain values on it
 84:         System.out.println("Checking the contents of these lists:");
 85:         if (myWords.contains("ten")) {
 86:             System.out.println("\t" + myWords + " contains a ten");
 87:         } else {
 88:             System.out.println("\t" + myWords + " DOESN'T contains a ten");
 89:         }
 90:         if (myWords.contains("hundred")) {
 91:             System.out.println("\t" + myWords + " contains a hundred");
 92:         } else {
 93:             System.out.println("\t" + myWords + " DOESN'T contains a hundred");
 94:         }

 96:         // check where those values are
 97:         System.out.println("\tThe word \"twenty\" appears at position "
 98:                 + myWords.indexOf("twenty") + " in " + myWords);
 99:         System.out.println("\tThe word \"hundred\" appears at position "
100:                 + myWords.indexOf("hundred") + " in " + myWords);
101:         pause();

103:         // remove by position
104:         System.out.println("Let's remove some items:");
105:         System.out.print("\t" + myWords + ".remove(0) makes myWords == ");
106:         myWords.remove(0);
107:         System.out.println(myWords);
108:         System.out.print("\t" + myWords + ".remove(3) makes myWords == ");
109:         myWords.remove(3);
110:         System.out.println(myWords);

112:         // remove by value
113:         myWords.remove("twenty");
114:         System.out.println("\tAfter removing \"twenty\" "
115:                 + "myWords is " + myWords);
116:         
117:         // remove by position and value in a list of Integers
118:         System.out.println("\tRemoving the item at position 1 "
119:                 + "of " + myInts + " leaves ");
120:         myInts.remove(1);
121:         System.out.println("\t\tmyInts == " + myInts);
122:         System.out.println("\tRemoving the the value 10 "
123:                 + "from " + myInts + " leaves ");
124:         myInts.remove(Integer.valueOf(10));
125:         System.out.println("\t\tmyInts == " + myInts);

127:         // remove by position and value in a list of Doubles
128:         System.out.println("\tRemoving the item at position 1 "
129:                 + "of " + myOtherDbls + " leaves ");
130:         myOtherDbls.remove(1);
131:         System.out.println("\t\tmyOtherDbls == " + myOtherDbls);
132:         System.out.println("\tRemoving the the value 20.0 "
133:                 + "from " + myOtherDbls + " leaves ");
134:         myOtherDbls.remove(20.0);
135:         System.out.println("\t\tmyOtherDbls == " + myOtherDbls);
136:         System.out.println("\tRemoving the the value 3927.5 "
137:                 + "from " + myOtherDbls + " leaves ");
138:         myOtherDbls.remove(3927.5);
139:         System.out.println("\t\tmyOtherDbls == " + myOtherDbls);
140:         pause();

142:         // check if a list is empty
143:         System.out.println("Let's check if lists are empty:");
144:         if (myWords.isEmpty()) {
145:             System.out.println("\t" + myWords + " is empty!");
146:         } else {
147:             System.out.println("\t" + myWords + " is NOT empty!");
148:         }
149:         
150:         // make a list empty (clear it out)
151:         myWords.clear();
152:         if (myWords.isEmpty()) {
153:             System.out.println("\t" + myWords + " is empty!");
154:         } else {
155:             System.out.println("\t" + myWords + " is NOT empty!");
156:         }
157:         pause();
158:     }

160:     private static final Scanner kbd = new Scanner(System.in);

162:     /**
163:      * Waits for the user to press pause (after prompting them to do so).
164:      */
165:     private static void pause() {
166:         System.out.println();
167:         System.out.print("...press enter...");
168:         kbd.nextLine();
169:         System.out.println();
170:     }
171: }