Self-Review Questions

  1. What is a Set? List? Map?

    A set is a data structure that contains at most one of any element.

    A list is a data structure that contains any number of any element.

    A map is a data structure that contains (key, value) pairs where a key is assigned to at most one value.

  2. What is an iterator and why are iterators necessary?

    An iterator is a type of object that is used to traverse a data structure.

  3. Why can't you make an object of type ArrayList<int>? What should you do instead?

    Because int is a primitive and not an Object. You should do ArrayList<Integer>.

  4. Implement a method that prints a given ArrayList<String> using an Iterator interface

    public void print(ArrayList<String> list){
    	Iterator<String> it = list.iterator();
    	while(it.hasNext()){
    		System.out.println(it.next());
    	}
    }
    
    
  5. Implement a method that prints a given ArrayList<String> using a for-each loop

    public void print(ArrayList<String> list){
    	for(String s : list)
    		System.out.println(s);
    }
    
  6. What are the advantages of using generics?

    type-checking support, more flexible code, code reuse.

  7. Are generics available at runtime?

    ArrayList<String> as a type does not exist