Home > Software design >  Java Arraylist Function Program
Java Arraylist Function Program

Time:06-07

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.

CodePudding user response:

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 mainand 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

  • Related