java:inner_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
java:inner_classes [2019/04/02 20:20]
gthanos
java:inner_classes [2019/04/02 20:38]
gthanos
Line 8: Line 8:
  
 <code java LinkedList.java> <code java LinkedList.java>
 +
 +/* A linked list with sentinels at head and tail.
 + */
 public class LinkedList<E> { public class LinkedList<E> {
   private Node<E> head, tail;   private Node<E> head, tail;
Line 27: Line 30:
     }     }
          
-    public Node<E> getNext() {return next;}+    public Node<E> getNext() { return next;}
     public void setNext(Node<E> nxt) { next = nxt; }     public void setNext(Node<E> nxt) { next = nxt; }
     public Node<E> getPrev() {return prev;}     public Node<E> getPrev() {return prev;}
Line 37: Line 40:
   private class Iterator<E> implements java.util.Iterator<E> {   private class Iterator<E> implements java.util.Iterator<E> {
     Node<E> curr;     Node<E> curr;
 +    
     public Iterator(Node<E> c) {     public Iterator(Node<E> c) {
       curr = c;       curr = c;
Line 52: Line 56:
       curr.getPrev().setNext(curr.getNext());       curr.getPrev().setNext(curr.getNext());
       curr.getNext().setPrev(curr.getPrev());       curr.getNext().setPrev(curr.getPrev());
-      curr = null;+      curr = curr.getPrev();
     }     }
   }   }
Line 97: Line 101:
  
 public class LinkedListUsage { public class LinkedListUsage {
-  public static final int SIZE = 20;+  public static final int SIZE = 16; 
 +  public static final int RANGE = 8 * SIZE;
      
   public static void main(String[] args) {   public static void main(String[] args) {
Line 103: Line 108:
     LinkedList<Integer> list = new LinkedList<>();     LinkedList<Integer> list = new LinkedList<>();
     for(int i=0; i<SIZE; i++) {     for(int i=0; i<SIZE; i++) {
-      list.add(rand.nextInt(8*SIZE));+      list.add(rand.nextInt(RANGE));
     }     }
     Iterator<Integer> it = list.iterator();     Iterator<Integer> it = list.iterator();
 +    
 +    // Print list
 +    while(it.hasNext()) {
 +      System.out.print(it.next()+"  ");
 +    }
 +    System.out.println();
 +    
 +    // Remove all elements less than RANGE/2
 +    System.out.println("Remove values < "+RANGE/2);
 +    it = list.iterator();
 +    while(it.hasNext()) {
 +      int value = it.next();
 +      //System.out.println("value: "+value);
 +      if(value < RANGE/2) {
 +        //System.out.println("RMV: "+value);
 +        it.remove();
 +      }
 +    }
 +    
 +    // Print list
 +    it = list.iterator();
     while(it.hasNext()) {     while(it.hasNext()) {
       System.out.print(it.next()+"  ");       System.out.print(it.next()+"  ");
Line 112: Line 138:
   }   }
 } }
 +
 </code> </code>
  
java/inner_classes.txt · Last modified: 2021/04/12 04:31 (external edit)