簡體   English   中英

Java Arraylist Function 程序

[英]Java Arraylist Function Program

我有這個 function:

public static void display(ArrayList<String> combinations) {
    
    int counter = 1;

    for (char c1 = 'a'; c1 <= 'z'; c1++) {
        for (char c2 = 'a'; c2 <= 'z'; c2++) {
            for (char c3 = 'a'; c3 <= 'z'; c3++) {
                String combo = "" + c1 + c2 + c3;                    
                combinations.add(combo);
                System.out.println("" + counter++ + " " + c1 + c2 + c3);
            }
        } 
    }

和這個:

public static void main(String[] args) throws IOException{
List<String> combinations = new ArrayList<>();
    System.out.println(combinations);

我想使用我開發並存儲在數組列表中的 function。 然后output將arraylist中存儲的數據提供給用戶使用函數/方法。

通過以下方式修改display方法:

  1. 將方法display返回類型從void更改為List<String>
  2. 返回combination列表。
public static  List<String> display(ArrayList<String> combinations){

//rest of your code

return combinations;
}

現在您所要做的就是調用方法 fromm main並將返回值分配給List變量。

List<String> combinations = display(new ArrayList<>());

您需要對什么是返回以及它的工作原理有一個基本的了解。 參考這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM