第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 ArrayList 作為參數(shù)的語(yǔ)法是什么?

使用 ArrayList 作為參數(shù)的語(yǔ)法是什么?

元芳怎么了 2023-10-12 16:57:02
基本上我想為我在 public static void main 中創(chuàng)建的“方法”創(chuàng)建一個(gè)單獨(dú)的方法。在這個(gè)方法中,我操作了一個(gè)數(shù)組列表,但不確定如何使用數(shù)組列表作為函數(shù)中的參數(shù)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);    }  }}我研究了語(yǔ)法,但找不到任何顯示語(yǔ)法的好視頻。
查看完整描述

2 回答

?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊

ArrayList實(shí)現(xiàn)List,因此您可以將其用作參數(shù),例如


public void testMethod(List<String> list){

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

}

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


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


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


查看完整回答
反對(duì) 回復(fù) 2023-10-12
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

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

    }


}

}


查看完整回答
反對(duì) 回復(fù) 2023-10-12
  • 2 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)