Java 如何在我的方法参数中传递 List?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38416546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass List in my method parameter?
提问by Jonathan Mousley
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.
有人可以解释如何定义 List 参数,以便我可以将 List 传递给它。我的方法看起来像这样。我需要替换 LIST,以便该方法将“listname”识别为列表。
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);
采纳答案by user2420211
Change the Method Definition to as following
将方法定义更改为如下
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);
}
回答by Gherbi Hicham
The Type should be a List<String>
there is no standard LIST Type in Java (unless you make it ofcourse).
Type 应该是List<String>
Java 中没有标准的 LIST 类型(除非你当然这样做)。