Logout

D.4.12

Trace algorithms using the implementations described in assessment statements D.4.9–D.4.11.

 

Teaching Note:

In examination questions, definitions of ArrayList and LinkedList methods will be given where necessary.

 

Sample Question:

sdfsdfsf

JSR Notes:


See the sample paper pages 43-46 for this, along with former papers to get practice with any kind of tracing.

As the teaching note mentions, you will be reminded what the methods used do. But here they are (and I'll include ArrayList too; even though it's not mentioned in the "JETS" document, it's mentioned in the assessment statements.).

***** For 2014, don't worry too much about the details of what follows; this is aimed more for using objects of these two classes in your dossiers, which, in fact you would have been allowed to to do instead of using arrays. For exam preparation, the main thing is to be able to understand the way objects of these classes could be used as seen in the sample paper. And that's basically adding or taking from a list. That is also seen in the example back on D.4.11.

 

LinkedList

Here's the list of methods to know for LinkedList, as found in the "JETS" document:

.add(E e)
.add(int index, E element)
.addFirst(E e)
.addLast(E e)
.clear()
element()
.get(int index)
.getFirst()
.getLast()
.remove()
.remove(int index)
.removeFirst()
.removeLast()
.size()
.isEmpty()

And below is the full list of methods that comes up in Netbeans. (Recall that the bold ones are from the class itself, and the non-bolded ones from inherited classes.)

Below, I'll put an example of these methods being used to implement our own Stack and Queue classes, but in preparation for exam questions, you should think to yourself, which of the above methods would be used to implement stacks and queues in particular.

linked list methods
LinkedList Methods


Stack & Queue Class Made from Inheriting LinkedList

(And here's a reminder of how inheritance works.)

 1 
 2 package toextendtostackandqueue;
 3 
 4 import java.util.LinkedList;
 5 
 6 /**
 7  *
 8  * @author John Rayworth
 9  */
10 public class MyStack extends LinkedList{
11     
12     public MyStack(){
13         
14     }
15     
16     public void push(Object o){
17         super.addLast(o);
18     }
19     
20     public Object pop(){
21         return super.getLast();
22     }
23 }
 1 
 2 package toextendtostackandqueue;
 3 
 4 import java.util.LinkedList;
 5 
 6 /**
 7  *
 8  * @author John Rayworth
 9  */
10 public class MyQueue extends LinkedList{
11     
12     public MyQueue(){
13         
14     }
15     
16     public void enqueue(Object o){
17         super.addLast(o);
18     }
19     
20     public Object dequeue(){
21         return super.getFirst();
22     }
23     
24 }

 

ArrayList

array list methods
ArrayList Methods