由泛型ArrayList支持的Java泛型类

由泛型ArrayList支持的Java泛型类,java,generics,Java,Generics,我编写了一个非常简单的类,名为Sack,它不按特定顺序保存一些数据,实际数据由ArrayList保存。我实现了这个类及其方法,一切看起来都很好,但是我的tester类中出现了编译时错误 麻袋类: 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 O

我编写了一个非常简单的类,名为
Sack
,它不按特定顺序保存一些数据,实际数据由ArrayList保存。我实现了这个类及其方法,一切看起来都很好,但是我的tester类中出现了编译时错误

麻袋类:

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();
  }
公共类Sack
{
//我怀疑这可能是罪魁祸首,不确定我是否能做到这一点
//但它编译得很好,这应该是Object类型吗?
ArrayList contents=新的ArrayList();
公共作废添加(E项)
{
目录。添加(项目);
}
公共布尔包含(E项)
{
返回内容。包含(项);
}
公共布尔删除(E项)
{
返回内容。删除(项目);
}
公共对象删除程序ANDOM()
{
if(isEmpty())
{
返回null;
}
其他的
{
int index=(int)(Math.random()*size());
返回内容。删除(索引);
}
}
公共整数大小()
{
返回contents.size();
}
公共布尔值为空()
{
返回内容。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));    
}
公共类驱动程序
{
Sack s=新Sack();
整数i=新整数(2);
s、 add(新整数(1));//
Sack s=new Sack();
整数i=新整数(2);
s、 add(新整数(1));//
Sack s=new Sack();
整数i=新整数(2);

s、 add(新整数(1));//您必须在方法或块中使用此代码,您不能在此处手动使用它

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

}
void测试(){

s、 add(新整数(1));//您必须在方法或块中使用此代码,您不能在此处手动使用它

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

}
void测试(){

s、 add(new Integer(1));//在名为main的静态方法中:“public static void main(String[]args)”谢谢..不确定我是如何忽略了这一点。在名为main的静态方法中:“public static void main(String[]args)”谢谢..不确定我是如何忽略了这一点。
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));

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


}