0

我想将 ArrayList 保存main()到我的新 ArrayList 中,它是 getTles(ArrayList input_list_of_strings) 中的子字符串。我怎么做?我知道我们需要处理循环并将其添加到 subString。此外,我想将代码中的字符串单词分解为小的子字符串。示例:“SCHOOLWORK”将分解为“SC”、“HOO”、“LWO”、“RK”。

import java.util.ArrayList;

public class HW2 {

    public static ArrayList getTiles(ArrayList input_list_of_strings) {
        // create a substring by go through the loop first, then .... (instruction)
        ArrayList<String> subString = new ArrayList<>();
        return input_list_of_strings;       
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<String> input_words = new ArrayList<String>();
        input_words.add("SCHOOLWORK");
        input_words.add("BALCONY");
        input_words.add("INSIST");
        input_words.add("SALTPETER");
        input_words.add("BOLTON");
        input_words.add("KITSCHY");
        input_words.add("CLIENTELE");
        System.out.print(getTiles(input_words));    
    }
}
4

1 回答 1

0

你没有保存任何东西。您将按ArrayList值传递到您的方法中。我还认为,设计接口而不是具体实现非常重要(也就是说,您将使用List它),并且您键入它以便您的方法中需要的行为不涉及强制转换或临时变量。

这就是我的意思。

public static List<String> getTiles(List<String> inputList) {
    // criteria for dividing up the strings themselves
    // logic to modify the list in-place or create a new copy to return
}
于 2013-02-01T06:43:57.100 回答