Guest User

Container.java

a guest
Apr 23rd, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. public class Container implements Iterable<Integer> {
  2.     private final List<Integer> list = new ArrayList<>();
  3.  
  4.     public void add(int i)
  5.         { list.add(i); }
  6.  
  7.     public int length()
  8.         { return list.size(); }
  9.    
  10.     public boolean isEmpty()
  11.         { return list.isEmpty(); }
  12.      
  13.     @Override public Iterator<Integer> iterator()
  14.         { return list.iterator(); }
  15.        
  16.     //////////////////////////////////////////////
  17.    
  18.     public static void main(String... args) {
  19.         Container c;
  20.        
  21.         //Everytime we want to make sure c contains an instance of Container
  22.         //We are forced to check against null
  23.         if (c != null) c = new Container();
  24.         //Even if we want to represent null as empty
  25.         boolean empty = c == null || c.isEmpty();
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment