我對某些應該非常簡單的事情有點麻煩。我不斷收到嘗試使用一種方法來改變我的ArrayList中的元素時,「太虛這裏不允許」錯誤不允許在這裏。更改ArrayList中的元素Java
public static void editDialog(int pos)
{
if(parcelList.get(pos).type==1)
{
JTextField field1 = new JTextField(""+parcelList.get(pos).idNum);
JTextField field2 = new JTextField(""+parcelList.get(pos).zone);
JTextField field3 = new JTextField(""+parcelList.get(pos).getWidth());
JTextField field4 = new JTextField();
JTextField field5 = new JTextField(""+parcelList.get(pos).getHeight());
Object[] fields =
{
"Please Enter New Box Details:\n", "\n",
"Box ID:", field1,
"Delivery Zone:", field2,
"Width:", field3,
"Length:", field4,
"Height:", field5
};
JOptionPane.showConfirmDialog(null, fields, "Amend Box Details:",
JOptionPane.OK_CANCEL_OPTION);
System.out.println(field1.getText() + "" + field2.getText() + "" + field3.getText() + "" + field4.getText()
+ "" + field5.getText());
String getZone = field2.getText();
int id = Integer.parseInt(field1.getText());
char zone = getZone.charAt(0);
int width = Integer.parseInt(field3.getText());
int length = Integer.parseInt(field4.getText());
int height = Integer.parseInt(field5.getText());
parcelList.set(pos,Parcel.newParcel.addBox(id, zone, width, length, height));
}
的addBox構造如下:
public void addBox(int id, char z, int w, int l, int h)
{
parcelList.add(new Box(id,z,w,l,h));
}
所有我需要做的是更改列表中特定位置的值。有誰知道爲什麼我得到這個錯誤?
包裹類
protected static ArrayList<Parcel> parcelList;
protected static ParcelList newParcel = new ParcelList();
public Parcel(int id, char z)
{
this.idNum = id;
this.zone = z;
}
//default contstructor
public Parcel()
{
this.idNum=0;
this.zone='z';
this.charge=0;
this.type=0;
this.i=new ImageIcon("images/nullParcel.jpg");
}
public double getCharge()
{
return charge;
}
public boolean isLarge()
{
return false;
}
public ImageIcon getImage()
{
return i;
}
@Override
public String toString()
{
return "Parcel{" + "idNum=" + idNum + ", charge=" + charge + ", zone=" + zone + '}';
}
public void setIdNum(int idNum) {
this.idNum = idNum;
}
public void setZone(char zone) {
this.zone = zone;
}
的ParcelList類:
ParcelList()
{
parcelList = new ArrayList<Parcel>();
}
public void addBox(int id, char z, int w, int l, int h)
{
parcelList.add(new Box(id,z,w,l,h));
}
public void removeItem(int b)
{
parcelList.remove(b);
}
public void addTube(int id, char z, int l)
{
parcelList.add(new Tube(id,z,l));
}
public void addEnvelope(int id, char z, char s)
{
parcelList.add(new Envelope(id, z, s));
}
public void printString()
{
for(Parcel p:parcelList)
{
System.out.println(p.toString()+"");
}
}
public boolean isFull()
{
if(parcelList.size()<=11)
{
return false;
}
return true;
}
public void clearList()
{
parcelList.clear();
}
public String listSize()
{
return ""+parcelList.size();
}
public boolean isEmpty()
{
return this.isEmpty();
}
@Override
public String toString() {
return "AssetList{" + "assetList=" + parcelList + '}';
}
}
你覺得'set'有什麼用?你認爲應該採取哪些論據?你爲什麼這麼認爲?你認爲錯誤的含義是什麼? –
我希望這個答案能幫助你。 http://stackoverflow.com/questions/15019723/void-type-not-allowed-here-java-error?rq=1 –
我認爲.set方法將設置當前元素爲我通過的任何新元素在?至於錯失即時消失,這就是爲什麼我張貼在這裏。 –