亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 ArrayList 作為參數的語法是什么?

使用 ArrayList 作為參數的語法是什么?

元芳怎么了 2023-10-12 16:57:02
基本上我想為我在 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);    }  }}我研究了語法,但找不到任何顯示語法的好視頻。
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

ArrayList實現List,因此您可以將其用作參數,例如


public void testMethod(List<String> list){

//....rest of your code goes here

}

始終記住,對象是通過引用傳遞的,因此您在此處的列表中所做的任何修改都將反映在調用該方法的方法中的列表中。


另外,對于您的代碼,java 支持菱形運算符,即您不需要在 的右側指定泛型類型=。左邊,即為了可維護性,參考變量應該是父接口,例如


List<String> list = new ArrayList<>();


查看完整回答
反對 回復 2023-10-12
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

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

    }


}

}


查看完整回答
反對 回復 2023-10-12
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號