How Can I Pass List in My Method Parameter?

Can someone explain how to define a List parameter such that i can pass a List to it. My method looks something like this. I need LIST to be replaced so the method recognizes "listname" as a list.

public static void function(int number, LIST listname) {
  for (int i = 0; i < listname.size(); ++i {
    System.out.print(listname.get(i) + ": ");
  }
  System.out.println(number);
}

In my main method I will call on the method as such:

List<String> myList = new ArrayList<String>();
  myList.add("item1");
  myList.add("item2");
  myList.add("item3");

function(4, myList);
5

2 Answers

Change the method definition to something as follows

public static void function(int number, List<String> listname) {
  for (int i = 0; i < listname.size(); ++i) {
    System.out.print(listname.get(i) + ": ");
  }
  System.out.println(number);
}
1

The Type should be a List<String> there is no standard LIST Type in Java (unless you make it ofcourse).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest