어떤 데이터를 특별한 순서없이 보유하는 매우 간단한 클래스 인 Sack
을 작성했습니다. 실제 데이터는 ArrayList에 의해 유지됩니다. 클래스와 메소드를 구현하고 모든 것이 잘 보였지만 테스터 클래스에는 컴파일 타임 오류가 발생합니다.Generic ArrayList에 의해 뒷받침되는 Java Generic 클래스
자루 클래스 :
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();
}
}
메인 클래스 :
SackDriver.java:11: error: <identifier> expected
s.add(x);
:
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));
}
이것은 내가 각 호출에 나타난 오류 (추가)이다 내가 여기서 뭘 잘못하고 있는지, 어떤 도움을 주시면 감사하겠습니다.
를 사용 : "공공 정적 무효 메인 (문자열 []에 args)" – Luciano
감사합니다 ... 나는 그것을 간과하는 방법을 확실하지. –