简体   繁体   中英

Java Arraylist Function Program

I have this function:

public static void display(ArrayList<String> combinations) {
    
    int counter = 1;

    for (char c1 = 'a'; c1 <= 'z'; c1++) {
        for (char c2 = 'a'; c2 <= 'z'; c2++) {
            for (char c3 = 'a'; c3 <= 'z'; c3++) {
                String combo = "" + c1 + c2 + c3;                    
                combinations.add(combo);
                System.out.println("" + counter++ + " " + c1 + c2 + c3);
            }
        } 
    }

And this:

public static void main(String[] args) throws IOException{
List<String> combinations = new ArrayList<>();
    System.out.println(combinations);

I want to use the function I developed and store in array list. Then output the data stored in the arraylist to the user using the function/method.

Modifiy the display method by:

  1. Change the method display return type from void to List<String> .
  2. return the combination list.
public static  List<String> display(ArrayList<String> combinations){

//rest of your code

return combinations;
}

And now all you have to to is just call the method fromm main and assign the returned value into a List variable.

List<String> combinations = display(new ArrayList<>());

You need have a basic idea what is return and how its work. Refer here

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