0

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.

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

2 Comments

In a static method called main: "public static void main(String[] args)"
Thank you.. Not sure how I overlooked that.
0

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)); 
        }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.