简体   繁体   中英

how do you pass arrays?

I have 2 classes. 1 class passes an ImageIcon[], and the other class takes the array and saves it.

These are the cut down versions of each class:

Class 1: monsterLabel[] is an ImageIcon[] saveLoad is is the name of Class2

ImageIcon[] toSave= new ImageIcon[button.length];
toSave[i]= new ImageIcon();
for (int i = 0; i < 99; i++) {
toSave[i]=(ImageIcon) monsterLabel[i].getIcon();
}
saveLoad.saver(toSave[]);

Class2(saveLoad)

public void saver(ImageIcon[] buttonPic) {}

The Problem is in class 1, Eclipse tells me

   "Syntax error on token "[", Expression expected after this token"

where i'm passing toSave[]

when I put in a number ie. toSave[0], it tells me that class2 is looking for a ImageIcon[] not an ImageIcon. How can I Pass an array?

Just like this:

saveLoad.saver(toSave);

The fact that it is an array is already part of the declared type, so this fact doesn't need to be reiterated. It'd be no different than if we had "int x". We wouldn't call someMethod(int x) - we'd just call someMethod(x) .

你可能只想要:

saveLoad.saver(toSave);

Replace saveLoad.saver(toSave[]); with saveLoad.saver(toSave);

You want to pass the variable, not part of the type with it.

You do not put [] at the end of an array when passing it as a value. It should be:

saveLoad.saver(toSave);

传递参数时,您不需要包含[]。

saveLoad.saver(toSave); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM