为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

绝地无双 2019-04-25 18:15:11
我正在递归地将字符串转换为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);     }}
查看完整描述

3 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

将单个ArrayList实例作为参数传递给您的方法(public static void strToList(String word, List<Character> letters)- 在这种情况下,您的方法不必返回任何内容),或者ArrayList按如下方式合并实例:

public static ArrayList<Character> strToList(String word){
    ArrayList<Character> letters = new ArrayList<Character>();
    if (!word.isEmpty()) {
        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); 
        letters.addAll(strToList(temp)); // add all the elements of the List returned by the
                                         // recursive call to the new List created in this 
                                         // call
    }
    return letters;}


查看完整回答
反对 回复 2019-05-15
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

更新您的代码如下

    public static ArrayList<Character> strToList(String word, ArrayList<Character> letters)
    {//        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, letters);
        }
    }
    public static void main(String[] args) throws Exception {
        ArrayList<Character> letters = new ArrayList<Character>();
        System.out.println(strToList("Hello", letters)); // [H, e, l, l, o]}

在递归中,您需要注意变量的范围。
在您的帖子中,由于您在每个调用堆栈上创建新的ArrayList,因此它没有来自前一个调用堆栈的数据。
因此,为了传递列表数据,我将其作为参数传递给函数。这将保留所有调用堆栈中的所有数据。


查看完整回答
反对 回复 2019-05-15
?
largeQ

TA贡献2039条经验 获得超7个赞

你也可以这样做


public static ArrayList<Character> strToCharList(String word) {

    ArrayList<Character> letters = new ArrayList<Character>();

    if (word.isEmpty() == true) 

        return letters;

     else 

        return (ArrayList<Character>) word.chars().mapToObj(character -> (char) character).collect(Collectors.toList());

}

要么


public static ArrayList<Character> strToCharList(String word) {

    return (word != null)? (ArrayList<Character>) word.chars().mapToObj(character -> (char) character).collect(Collectors.toList()): new ArrayList<Character>();

}

方法调用


public static void main(String[] args) {

    System.out.println(strToList("Hello"));

}

输出:


[H, e, l, l, o]


查看完整回答
反对 回复 2019-05-15
  • 3 回答
  • 0 关注
  • 556 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号