[英]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
方法:
display
返回類型從void
更改為List<String>
。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.