简体   繁体   中英

How can I pass a variable of type array

How can I pass a variable of type array as a parameter in an instantiated object

I have this on the Main.java Class :

String [] itemList;

Magazine magazine1 = new Magazine(nMagazine, itemList[], distributionPrice, pvp);

This the Magazine.java Class

public class Magazine extends Publication {

 private int numberMagazine;
 private String[] itemList;


public Magazine(int numberMagazine, String[] itemList, float distributionPrice, float pvp) {
    super(distributionPrice, pvp);
    

    this.numberMagazin = numberMagazin;
    this.itemList = itemList;

}

In the main class when I instantiate the magazine1 object I can't call the itemList[] variable and I don't understand why

You need to remove the braces [] when you reference the array. Instead of itemList[] it should be just itemList like this:

String [] itemList;

Magazine magazine1 = new Magazine(nMagazine, itemList, distributionPrice, pvp);

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