简体   繁体   English

如何在我的方法参数中传递 List?

[英]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.有人可以解释如何定义 List 参数,以便我可以将 List 传递给它。 My method looks something like this.我的方法看起来像这样。 I need LIST to be replaced so the method recognizes "listname" as a 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);

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);
}

类型应该是一个List<String> Java 中没有标准的 LIST 类型(除非你当然这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将参数的类传递给另一个方法调用? - How can I pass the class of my parameter to another method call? 如何将JPanel作为参数传递给方法? - How can I pass a JPanel as a parameter to a method? 如何动态构建列表并将其作为方法参数传递? - How do I Construct a list on the fly and pass it as a method parameter? 如何将带有&#39;/&#39;符号的值作为get参数传递给控制器​​? - How can I pass a value with '/' symbol as get parameter to my controller? 我可以将 List 作为参数传递给 MyBatis 映射器吗? - Can I pass a List as a parameter to a MyBatis mapper? 我可以在JSP的URL中将列表作为参数传递吗? - Can I pass a List as parameter in a URL in JSP? 我可以将列表作为参数传递给Scala / Java中的方法吗,该方法是通过以下方式定义的:detectFormatAutomatically(delimitersForDetection:Char *) - Can i pass a list as a parameter to a method in Scala/Java which is defined this way detectFormatAutomatically(delimitersForDetection:Char*) 如何将列表的内容传递给varargs方法? - How can I pass the contents of a list to a varargs method? 当我的方法不在活动中时,如何将其传递给活动? - How can I pass my method an activity when it is not in an activity? 如何将通用参数传递给方法? - How do I pass a generic parameter to a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM