Sack
いくつかのデータを特定の順序で保持しないという非常に単純なクラスを作成しました。実際のデータはArrayListによって保持されます。クラスとそのメソッドを実装したところ、すべて問題なく見えましたが、テスタークラスでコンパイル時エラーが発生しました。
サッククラス:
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();
}
}
メインクラス:
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));
}
これは、add()を呼び出すたびに受け取るエラーです。
SackDriver.java:11: error: <identifier> expected
s.add(x);
私がここで何を間違っているのかわからないので、助けていただければ幸いです。