使用 ArrayList 作为参数的语法是什么?

基本上我想为我在 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);

    }

  }

}

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


元芳怎么了
浏览 91回答 2
2回答

翻阅古今

ArrayList实现List,因此您可以将其用作参数,例如public void testMethod(List<String> list){//....rest of your code goes here}始终记住,对象是通过引用传递的,因此您在此处的列表中所做的任何修改都将反映在调用该方法的方法中的列表中。另外,对于您的代码,java 支持菱形运算符,即您不需要在 的右侧指定泛型类型=。左边,即为了可维护性,参考变量应该是父接口,例如List<String> list = new ArrayList<>();

慕莱坞森

you just need something like belowimport java.util.ArrayList;public class ReverseArrayList{&nbsp; public static void main(String[] args) {&nbsp; &nbsp; //&nbsp; Note: I used a sample array with 6 elements.&nbsp;&nbsp; &nbsp; //&nbsp; I explain the code as if I am strictly using 6 elements&nbsp; &nbsp; //&nbsp; However this may be done with any # of elements&nbsp;&nbsp; &nbsp; ArrayList<String> reverseMe = new ArrayList<String>();&nbsp; &nbsp; reverseMe.add("I");&nbsp; &nbsp;&nbsp; &nbsp; reverseMe.add("am");&nbsp; &nbsp; reverseMe.add("going");&nbsp; &nbsp; reverseMe.add("to");&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; reverseMe.add("be");&nbsp; &nbsp; reverseMe.add("reversed");&nbsp; &nbsp; reverseList(reverseMe);&nbsp; }private static void reverseList(ArrayList<String> arrayList){&nbsp;//&nbsp; This loop will run until we reach the midpoint of the array. At the midpoint, all the elements would be reversed&nbsp; &nbsp; for (int i = 0; i < reverseMe.size()/2; i++){&nbsp; &nbsp; &nbsp; //&nbsp; Save the first three values for later use.&nbsp;&nbsp; &nbsp; &nbsp; String initial = reverseMe.get(i);&nbsp; &nbsp; &nbsp; //&nbsp; The 1st element will be assigned to the last element, upto the midpoint&nbsp;&nbsp; &nbsp; &nbsp; reverseMe.set(i, reverseMe.get(reverseMe.size() - i - 1));&nbsp; &nbsp; &nbsp; //&nbsp; The last element will be assigned to the 1st element, upto the midpoint&nbsp; &nbsp; &nbsp; reverseMe.set(reverseMe.size() - i - 1, initial);&nbsp; &nbsp; }&nbsp; &nbsp; //&nbsp; Displays the contents of the arraylist&nbsp; &nbsp; for(String i: reverseMe){&nbsp; &nbsp; &nbsp; System.out.println(i);&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java