Practice Exercise

 

1.       Review class ArrayList from http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html . Look at the operations that you can do on ArrayList. What operations of ArrayList cannot be performed on an array? What operations CAN be performed on an array as well?

2.       Go to http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html . What are some operations that can be done on an arraylist as well?

3.       Draw a single-linked list of Integer objects containing integers 5, 10, 7. Suppose the class representing each node of this list is named LLIntegerNode and the list is named numbers. 

a.       Write the data fields that LLIntegerNode class has. Also write two constructor methods.

b.      Complete the following code fragment that aims at adding all integers in the list.

    int sum = 0;

   LLIntegerNode temp = _______________;

  while (temp != null){

     int count = _______________________;

    sum += count;

  temp = _____________;

}

4.       Consider the single-linked list numbers of Integer objects that you created in the previous question. Explain the effect of each code statement of that list. (Draw the modified list in each case).

a.       numbers =  new LLIntegerNode(12, numbers.next);

b.      LLIntegerNode refNode = numbers.next;

refNode.next = refNode.next.next;

                                          

c.       LLIntegerNode refNode = numbers;

while(refNode.next != null)

      refNode = refNode.next;

refNode.next = new LLIntegerNode(15);