Hunter McMillen
Hunter McMillen

Reputation: 61512

Java Generic class backed by Generic ArrayList

I wrote a really simple class called Sack that holds some data in no particular order, the actual data is held by an ArrayList. I implemented the class and its methods and everything looked fine to me, but I receive compile-time errors in my tester class.

Sack class:

public class Sack<E>
{
  //I suspect this might be the culprit, not sure if I can do this
  //but it compiles fine, should this maybe be of type Object?
  ArrayList<E> contents = new ArrayList<E>(); 

  public void add(E item)
  {
     contents.add(item);
  }

  public boolean contains(E item)
  {
     return contents.contains(item);              
  }

  public boolean remove(E item)
  {
     return contents.remove(item);
  }

  public Object removeRandom()
  {
     if(isEmpty())
     {
        return null;
     }
     else
     {
        int index = (int)(Math.random() * size());
        return contents.remove(index);
     }
  }

  public int size()
  {
     return contents.size();
  }

  public boolean isEmpty()
  {
     return contents.isEmpty();
  }

}

Main class:

public class SackDriver
{
    Sack<Integer> s = new Sack<Integer>();
    Integer i       = new Integer(2);

    s.add(new Integer(1)); //<- Error
    s.add(i);              //<- Error
    s.add(3);              //<- Error
    s.add(4);              //<- Error
    s.add(5);              //<- Error
    s.add(6);              //<- Error

    System.out.println("Size: " + s.size() + " Contains: " + s.contains(5));    
}

This is the error I receive on each call to add():

SackDriver.java:11: error: <identifier> expected
                    s.add(x);

Not sure what I am doing wrong here, any help would be appreciated.

Upvotes: 0

Views: 439

Answers (2)

Shehzad
Shehzad

Reputation: 2940

you have to use this code in method or blocks you can not manuplate it here. i.e

void test(){
s.add(new Integer(1)); //<- Error     s.add(i);              //<- Error     s.add(3);              //<- Error     s.add(4);              //<- Error     s.add(5);              //<- Error     s.add(6);              //<- Error      System.out.println("Size: " + s.size() + " Contains: " + s.contains(5));

}

Or use

class SackDriver {    



        Sack<Integer> s = new Sack<Integer>();     
        Integer i       = new Integer(2);     
        {
        s.add(new Integer(1)); 
        //<- Error     
        s.add(i);              
        //<- Error     
        s.add(3);             
        //<- Error    
        s.add(4);            
        //<- Error    
        s.add(5);             
        //<- Error    
        s.add(6);              
        //<- Error     
        System.out.println("Size: " + s.size() + " Contains: " + s.contains(5)); 
        }


}

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198023

Sack<Integer> s = new Sack<Integer>();
Integer i       = new Integer(2);

s.add(new Integer(1)); //<- Error
s.add(i);              //<- Error
s.add(3);              //<- Error
s.add(4);              //<- Error
s.add(5);              //<- Error
s.add(6);              //<- Error

System.out.println("Size: " + s.size() + " Contains: " + s.contains(5)); 

This needs to be in a method, constructor, or static block, not just hanging out in the class. It has nothing to do with generics.

Upvotes: 4

Related Questions