【问题标题】:What is the syntax for using an ArrayList as a parameter?使用 ArrayList 作为参数的语法是什么?
【发布时间】:2019-10-09 07:42:33
【问题描述】:

基本上我想为我在 public static void main 中创建的“方法”创建一个单独的方法。

在这种方法中,我操作了一个数组列表,但不确定如何将数组列表用作函数中的参数

import java.util.ArrayList;

public class ReverseArrayList{
  public static void main(String[] args) {
    //  Note: I used a sample array with 6 elements. 
    //  I explain the code as if I am strictly using 6 elements
    //  However this may be done with any # of elements 
    ArrayList<String> reverseMe = new ArrayList<String>();
    reverseMe.add("I");   
    reverseMe.add("am");
    reverseMe.add("going");
    reverseMe.add("to");      
    reverseMe.add("be");
    reverseMe.add("reversed");

    //  This loop will run until we reach the midpoint of the array. At the midpoint, all the elements would be reversed
    for (int i = 0; i < reverseMe.size()/2; i++){

      //  Save the first three values for later use. 
      String initial = reverseMe.get(i);

      //  The 1st element will be assigned to the last element, upto the midpoint 
      reverseMe.set(i, reverseMe.get(reverseMe.size() - i - 1));

      //  The last element will be assigned to the 1st element, upto the midpoint
      reverseMe.set(reverseMe.size() - i - 1, initial);
    }
    //  Displays the contents of the arraylist
    for(String i: reverseMe){
      System.out.println(i);
    }
  }
}

我研究了语法,但找不到任何显示语法的好视频。

【问题讨论】:

  • 它与使用任何其他数据类型完全相同。你在说什么?
  • 将数组列表作为方法参数传递的语法与将任何其他类型作为参数传递的语法相同。你尝试了什么,遇到了什么困难?
  • 了解可变参数。如果您不知道需要多少参数,这是一个不错的选择
  • 例如,如果你想使用一个数组作为参数,你会这样做:int x[] 作为参数,那么arraylists呢?

标签: java arrays syntax


【解决方案1】:

ArrayList 实现了List,因此您可以将其用作参数,例如

public void testMethod(List<String> list){
//....rest of your code goes here
}

始终记住对象是通过引用传递的,因此您在此处对列表所做的任何修改都将反映在调用此方法的方法列表中。

另外,对于您的代码,java 支持菱形运算符,即您不需要在 = 的右侧指定泛型类型。在左侧,即参考变量应该是父接口,以便维护,例如

List&lt;String&gt; list = new ArrayList&lt;&gt;();

【讨论】:

    【解决方案2】:
    you just need something like below
    
    import java.util.ArrayList;
    
    public class ReverseArrayList{
      public static void main(String[] args) {
        //  Note: I used a sample array with 6 elements. 
        //  I explain the code as if I am strictly using 6 elements
        //  However this may be done with any # of elements 
        ArrayList<String> reverseMe = new ArrayList<String>();
        reverseMe.add("I");   
        reverseMe.add("am");
        reverseMe.add("going");
        reverseMe.add("to");      
        reverseMe.add("be");
        reverseMe.add("reversed");
        reverseList(reverseMe);
      }
    private static void reverseList(ArrayList<String> arrayList){
     //  This loop will run until we reach the midpoint of the array. At the midpoint, all the elements would be reversed
        for (int i = 0; i < reverseMe.size()/2; i++){
    
          //  Save the first three values for later use. 
          String initial = reverseMe.get(i);
    
          //  The 1st element will be assigned to the last element, upto the midpoint 
          reverseMe.set(i, reverseMe.get(reverseMe.size() - i - 1));
    
          //  The last element will be assigned to the 1st element, upto the midpoint
          reverseMe.set(reverseMe.size() - i - 1, initial);
        }
        //  Displays the contents of the arraylist
        for(String i: reverseMe){
          System.out.println(i);
        }
    
    }
    }
    

    【讨论】:

    • 我认为方法声明中缺少关键字“void”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2012-06-22
    • 2014-10-20
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多