1

How do I also pass the arraylist 'end' as a parameter inside the begin method along with the 'start' arraylist?

List<String> start = new ArrayList<String>();
List<String> end = new ArrayList<String>(); 

public static void begin(List<String> start)
{

}
1
  • 4
    Give the method a second formal parameter List<String> end. For more information on how to define methods, I recommend reading a tutorial on the topic, e.g. this one by Oracle. Commented Jun 30, 2021 at 20:22

3 Answers 3

2

Just simply add it to the parameter list of the method:

List<String> start = new ArrayList<String>();
List<String> end = new ArrayList<String>(); 

public static void begin(List<String> start, List<String> end) {

}

and call the method like this:


begin(start, end);
Sign up to request clarification or add additional context in comments.

Comments

1

Just add it as a second parameter.

List<String> startList = new ArrayList<String>();
List<String> endList = new ArrayList<String>(); 
begin(startList, endList); // the actual call

...
public static void begin(List<String> start, List<String> start)
{

}

1 Comment

0

While multiple parameters is definitely the recommended way to do it, there are a few alternative non-recommended ways to achieve this as well. Enjoy!

import java.util.List;

public class ParameterListTest {

  public static void main(String[] args) {
    List<String> startList = List.of("Value1", "Value2");
    List<String> endList = List.of("Value3", "Value4"); 
    
    //Recommended way
    version1(startList, endList);
    
    //Goofy alternatives
    version2(startList, endList);
    
    List<String>[] listArray = new List[]{startList, endList};
    version3(listArray);
    
    List<List<String>> nestedList = List.of(startList, endList);
    version4(nestedList);
  }
  
  public static void version1(List<String> start, List<String> end) {
    System.out.println("Version 1");
    System.out.println(start);
    System.out.println(end);
    System.out.println();
  }
  
  public static void version2(List<String>...lists) {
    System.out.println("Version 2");
    System.out.println(lists[0]);
    System.out.println(lists[1]);
    System.out.println();
  }
  
  public static void version3(List<String>[] lists) {
    System.out.println("Version 3");
    System.out.println(lists[0]);
    System.out.println(lists[1]);
    System.out.println();
  }
  
  public static void version4(List<List<String>> nest) {
    System.out.println("Version 4");
    System.out.println(nest.get(0));
    System.out.println(nest.get(1));
    System.out.println();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.