import java.util.*; public class Main { public static void main(String[] args) { LinkedList ll = new LinkedList(); // add to linked list ll.add("first String object"); // total items in the linked list System.out.println(ll.size()); // indicates: 1 // the list is not type specific // Strings and ints can coexist ll.add(22); // add ten more items for (int i=0; i<10; i++){ ll.add(i); } String oType = ""; // iterate through all existing items // with for-each for(Object o:ll){ oType = o.getClass().toString(); System.out.println(o.toString() + ", is type: " + oType); } // remove items from list ll.removeFirstOccurrence(3); // remove the first item from the list ll.remove(); // remove the last item from the list ll.removeLast(); } }
Showing posts with label Object. Show all posts
Showing posts with label Object. Show all posts
Friday, September 25, 2009
Java - how to create and use a LinkedList
Labels:
add,
for,
foreach,
getClass,
java,
LinkedList,
Object,
remove,
removeFirstOccurence,
removeLast
Thursday, September 24, 2009
Java - how to create and use a Vector
import java.util.*; public class Main { public static void main(String[] args) { Vector v = new Vector(); // The Vector class is almost identical to the // ArrayList class. The main difference is that // a Vector is thread safe. v.add("number one is a String"); v.add(22); // number two is an Integer for (int i=0; i<10; i++){ // add ten integers v.add(i); } // iterate through the vector with a foreach for (Object o:v){ System.out.println(o); } // as with ArrayLists you'll notice that // Vectors are not type specific. You can // put in whatever you want } }
Subscribe to:
Posts (Atom)