每次运行时都会创建一个新的数组列表

我正在递归地将字符串转换为Character ArrayList。每次调用函数时都会创建一个新的ArrayList。

public static ArrayList<Character> strToList(String word){
    ArrayList<Character> letters = new ArrayList<Character>();
    //ArrayList<Character> emptyList = new ArrayList<Character>();
    if(word.isEmpty() == true){
        return letters;        
    }
    else {
        char let = word.charAt(0);
        letters.add(Character.valueOf(let));
        System.out.println(letters);
        String temp = word.substring(1, word.length());
        System.out.println(temp);
        return strToList(temp);
    }}


绝地无双
浏览 557回答 3
3回答

潇潇雨雨

将单个ArrayList实例作为参数传递给您的方法(public static void strToList(String word, List<Character> letters)- 在这种情况下,您的方法不必返回任何内容),或者ArrayList按如下方式合并实例:public&nbsp;static&nbsp;ArrayList<Character>&nbsp;strToList(String&nbsp;word){ &nbsp;&nbsp;&nbsp;&nbsp;ArrayList<Character>&nbsp;letters&nbsp;=&nbsp;new&nbsp;ArrayList<Character>(); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!word.isEmpty())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;let&nbsp;=&nbsp;word.charAt(0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letters.add(Character.valueOf(let)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(letters); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;temp&nbsp;=&nbsp;word.substring(1,&nbsp;word.length()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(temp);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letters.addAll(strToList(temp));&nbsp;//&nbsp;add&nbsp;all&nbsp;the&nbsp;elements&nbsp;of&nbsp;the&nbsp;List&nbsp;returned&nbsp;by&nbsp;the &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;recursive&nbsp;call&nbsp;to&nbsp;the&nbsp;new&nbsp;List&nbsp;created&nbsp;in&nbsp;this&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;call &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;letters;}

慕妹3146593

更新您的代码如下&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;ArrayList<Character>&nbsp;strToList(String&nbsp;word,&nbsp;ArrayList<Character>&nbsp;letters) &nbsp;&nbsp;&nbsp;&nbsp;{//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList<Character>&nbsp;letters&nbsp;=&nbsp;new&nbsp;ArrayList<Character>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//ArrayList<Character>&nbsp;emptyList&nbsp;=&nbsp;new&nbsp;ArrayList<Character>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(word.isEmpty()&nbsp;==&nbsp;true){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;letters; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;let&nbsp;=&nbsp;word.charAt(0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;letters.add(Character.valueOf(let)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(letters); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;temp&nbsp;=&nbsp;word.substring(1,&nbsp;word.length()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(temp); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;strToList(temp,&nbsp;letters); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList<Character>&nbsp;letters&nbsp;=&nbsp;new&nbsp;ArrayList<Character>(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(strToList("Hello",&nbsp;letters));&nbsp;//&nbsp;[H,&nbsp;e,&nbsp;l,&nbsp;l,&nbsp;o]}在递归中,您需要注意变量的范围。在您的帖子中,由于您在每个调用堆栈上创建新的ArrayList,因此它没有来自前一个调用堆栈的数据。因此,为了传递列表数据,我将其作为参数传递给函数。这将保留所有调用堆栈中的所有数据。

largeQ

你也可以这样做public static ArrayList<Character> strToCharList(String word) {&nbsp; &nbsp; ArrayList<Character> letters = new ArrayList<Character>();&nbsp; &nbsp; if (word.isEmpty() == true)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return letters;&nbsp; &nbsp; &nbsp;else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return (ArrayList<Character>) word.chars().mapToObj(character -> (char) character).collect(Collectors.toList());}要么public static ArrayList<Character> strToCharList(String word) {&nbsp; &nbsp; return (word != null)? (ArrayList<Character>) word.chars().mapToObj(character -> (char) character).collect(Collectors.toList()): new ArrayList<Character>();}方法调用public static void main(String[] args) {&nbsp; &nbsp; System.out.println(strToList("Hello"));}输出:[H, e, l, l, o]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java