a1.add(2,new Bread(1, "荷叶", "小", "馒头"));
a1.add(1,new Bread(2, "飘香", "中", "蛋黄派"));
a1.add(0,new Bread(3, "巨无霸", "大", "汉堡包"));
按0,1,2来添加就没事,可是一调过来就异常,为什么呢?
代码:
public class Bread
{
int num;
String brand;
String size;
String type;
public Bread(int num, String brand, String size, String type) {
super();
this.num = num;
this.brand = brand;
this.size = size;
this.type = type;
}
public String toString() {
return num+":"+brand+size+type;
}
}
import java.util.ArrayList;
import java.util.LinkedList;
public class Example {
public static void main(String[] args)
{
LinkedList<Bread> a1=new LinkedList<Bread>();
a1.add(2,new Bread(1, "荷叶", "小", "馒头"));
a1.add(1,new Bread(2, "飘香", "中", "蛋黄派"));
a1.add(0,new Bread(3, "巨无霸", "大", "汉堡包"));
for(int j=0;j<a1.size();j++)
{
Bread ss=a1.get(j);
System.out.println(ss);
}
}
}
幕布斯6054654
Cats萌萌
Helenr
相关分类