Java

TOPIC 19 – JAVA COLLECTIONS I - LISTS

 

LESSON WORK

 

 

QUESTION 1 (GROUP WORK)


You will work with an ArrayList structure.  It is an implementation of a List structure that is created using an array.  Of course, the implementation itself doesn't matter at all for simple tasks.  It can however have a big impact on the efficiency of any program using the list.

 

 

Do the following:

 

a)                      Create an ArrayList<Integer> object named al.

 

b)                      Add five elements to the list.

 

c)                      Output the list to screen to verify its content. 

 

d)                      Output the size of the list to screen by using the size() method.

 

e)                      Add another element at the beginning of the list.

 

f)                        Output the list to screen to verify its content.

g)                      Add another element at the end of the list.

h)                      Output the list to screen to verify its content.

i)                        Add another element after element index 3 (at element index 4).

j)                        Output the list to screen to verify its content.

k)                      Remove the element with index 2.

 

l)                        Output the list to screen to verify its content.

m)                    Remove the first element.

 

n)                      Output the list to screen to verify its content.

 

o)                      Remove the last element.

 

p)                      Output the list to screen to verify its content.

 

q)                      Loop through the list and increase each element's value by 1.

 

r)                        Output the list to screen to verify its content.


QUESTION 2

You will now work with the LinkedList class.  Again, this is an implementation of a List data structure.  However, instead of using an array, it uses individual Node objects that reference each other.  We will learn more about these Node objects later on.

 

Do the same steps as you did in Question 1 but with the LinkedList class instead. 

 

Notice that the functionality is the same because both ArrayList and LinkedList implement the List interface.