简体   繁体   中英

(Beginner Java) Main method refers to a method in a seperate class which holds arrays. How would the method in the second class be set up?

My main method has: the arrays

String[] numbers = {"1", "2", "3", "4", "5", "6", "7"};
String[] brands= {"aa", "bb", "cc", "dd", "ee", "ff", "gg"};
String[] types= {"hh", "ii", "jj", "kk", "ll", "mm", "nn"};

a reference to my second method (in the same package with class name Soda)

 Soda[] list = new Soda[numbers.length];
 list = listOfSodaCans(numbers,brands,types);

I know my listOfSodaCans method in my Soda class has to receive the parameters in the form of (java.lang.String[],java.lang.String[],java.lang.String[]) but I can't seem to get this set up. I repeatedly get a cannot find symbol. Does anyone have some advice on what a general structure would look like for the listOfSodaCans method?

Thank you!

@Alice- my Soda method:

public class Soda {

private Soda[] listOfSodaCans;


          public Soda[] getListOfSodaCans() {
           return listOfSodaCans;
       }

 public void setListOfSodaCans(Soda[] listOfSodaCans) {
       this.listOfSodaCans= listOfSodaCans;
        }

    public static Soda[] listOfSodaCans(String[] numbers, String[] brands,
            String[] types) {
        // more code
        return null;
    }
}

Here is something that works. I use the "main" class (so only one) to simplify the code, but it shall be the same.

import java.io.Console;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] numbers = {"1", "2", "3", "4", "5", "6", "7"};
        String[] brands= {"aa", "bb", "cc", "dd", "ee", "ff", "gg"};
        String[] types= {"hh", "ii", "jj", "kk", "ll", "mm", "nn"};

        Main m = new Main();

        m.listOfSodaCans(numbers, brands, types);
    }


    public void listOfSodaCans(String[] numbers, String[] brands, String[] types) {
        System.out.println(numbers.length);
    }

}

Soda Class

 public class Soda {
    public static Soda[] listOfSodaCans(String[] numbers, String[] brands,
            String[] types) {
        // more code
        return null;
    }
}

Main Class will like that

public static void main(String[] args) {

        String[] numbers = { "1", "2", "3", "4", "5", "6", "7" };
        String[] brands = { "aa", "bb", "cc", "dd", "ee", "ff", "gg" };
        String[] types = { "hh", "ii", "jj", "kk", "ll", "mm", "nn" };

        Soda[] list = new Soda[numbers.length];
        list = Soda.listOfSodaCans(numbers, brands, types);

        // way 2, accessing by instance
        // Soda soda = new Soda();
        // list = soda.listOfSodaCans(numbers, brands, types);
    }

If you use way 2, you better remove static from listOfSodaCans()

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