## August 9, 2008

Create and populate a List. Create a second List of the same size as the first, and use ListIterators to read elements from the first List and insert them into the second in reverse order.

In Java:
`package chapter.holdingYourObjects;import java.util.*;public class Exercise12 {    public static void main(String[] args) {        List<Integer> x = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));        List<Integer> y = new ArrayList<Integer>(x);                ListIterator<Integer> it = x.listIterator();        while(it.hasNext()) {            y.set(x.size() - it.nextIndex() - 1, it.next());        }                System.out.println("x = " + x);        System.out.println("y = " + y);    }}/* Output:* x = [1, 2, 3, 4, 5]* y = [5, 4, 3, 2, 1]*/`

In Python:
`x = [1, 2, 3, 4, 5]y = x[:]y.reverse()print "x = " + str(x)print "y = " + str(y)`

In Lisp:
`(defparameter *x* '(1 2 3 4 5))(defparameter *y* (reverse *x*))(format t "x = ~s~%" *x*)(format t "y = ~s"  *y*)`

Create a class, then make an initialized array of objects of your class. Fill a List from your array. Create a subset of you List by using subList(), then remove this subset from your list.

In Java:
`package chapter.holdingYourObjects;import java.util.*;class MyClass {}public class Exercise7 {    public static void main(String[] args) {        MyClass[] myClassArray = {new MyClass(), new MyClass(), new MyClass()};        List<MyClass> myClassList = new ArrayList<MyClass>(Arrays.asList(myClassArray));                // Can't use sublist because removeall won't work        List<MyClass> sublist = Arrays.asList(myClassList.get(0), myClassList.get(1));                System.out.println("myClassList = " + myClassList);        System.out.println("sublist = " + sublist);                myClassList.removeAll(sublist);                System.out.println("myClassList = " + myClassList);    }}/* Output:myClassList = [chapter.holdingYourObjects.MyClass@3e25a5, chapter.holdingYourObjects.MyClass@19821f, chapter.holdingYourObjects.MyClass@addbf1]sublist = [chapter.holdingYourObjects.MyClass@3e25a5, chapter.holdingYourObjects.MyClass@19821f]myClassList = [chapter.holdingYourObjects.MyClass@addbf1]*/`

In Python:
`class MyClass():    passmyClassArray = [MyClass(), MyClass(), MyClass()]sublist = myClassArray[0:2]print "myClassArray = " + str(myClassArray)print "sublist = " + str(sublist)for item in sublist:    myClassArray.remove(item)print "myClassArray = " + str(myClassArray)#>>> ## working on region in file /tmp/python-7533hFb...#myClassArray = [<__main__.MyClass instance at 0xb7d9ee8c>, <__main__.MyClass instance at 0xb7d9ef0c>, <__main__.MyClass instance at 0xb775830c>]#sublist = [<__main__.MyClass instance at 0xb7d9ee8c>, <__main__.MyClass instance at 0xb7d9ef0c>]#myClassArray = [<__main__.MyClass instance at 0xb775830c>]`

In Lisp:
`(defclass MyClass () ())(defparameter *MyClass-array* (list (make-instance 'MyClass)                    (make-instance 'MyClass)                    (make-instance 'MyClass)))(defparameter *sublist* (butlast *MyClass-array* 1))(format t "MyClass = ~s~%" *MyClass-array*)(format t "sublist = ~s~%" *sublist*)(loop for item in *sublist* do       (setf *MyClass-array* (remove item *MyClass-array*)))(format t "MyClass = ~s~%" *MyClass-array*);;;Output:;MyClass = (#<MYCLASS #x20513866> #<MYCLASS #x20513876> #<MYCLASS #x20513886>);sublist = (#<MYCLASS #x20513866> #<MYCLASS #x20513876>);MyClass = (#<MYCLASS #x20513886>)`