简体   繁体   English

reverse 方法应该反向打印出 lst 。 它改为打印一个空的 arrayList。 为什么会发生这种情况,我该如何解决?

[英]The reverse method should print out lst in reverse. It prints an empty arrayList instead. Why is this happening and how do I fix it?

This is the class with the buildList method which builds the in这是带有 buildList 方法的 class

class Recursive 
{
    public static ArrayList<Integer> reversedList = new ArrayList<Integer>();

    public static ArrayList<Integer> buildList(int n)//builds the arrayList based on integer. If the int is 5 then the contents are 1,2,3,4,5.
    {
        // write this in terms of a recursive call using a smaller n        
        ArrayList<Integer> tempList = null;
        if (n <= 0) // The smallest list we can make
        {  
        return new ArrayList<Integer>(); 

        }
        else // All other size lists are created here
        {

        tempList= buildList(n-1);
        tempList.add(n);
        }



        return tempList;

    }

This is the problem method.这就是问题方法。 Idk why it returns [], I think there is a passing error. Idk 为什么它返回 [],我认为有一个传递错误。

    public static ArrayList<Integer> reverse(ArrayList<Integer> lst)//the problem method
    {
        if(lst.size()<=0) {

        }
        else {
            reversedList.add(lst.remove(lst.size()-1)); 
            reverse(lst);

        }
        return reversedList;

    }
    public static void main(String[] args)
    {
        ArrayList<Integer> lst = Recursive.buildList(5);
        System.out.println(lst);
        reverse(lst);
        System.out.println(lst);


    }

}

The reverse method removes the last item of lst and adds it to the empty reversedList. reverse 方法删除 lst 的最后一项并将其添加到空的 reversedList 中。 After the first iteration the contents should be [5].第一次迭代后,内容应为 [5]。 Second [5,4]...all the way to [5,4,3,2,1].第二个 [5,4]...一直到 [5,4,3,2,1]。 But somehow it ends up being [].但不知何故,它最终成为[]。 So the reverse method is supposed to print out [5,4,3,2,1] but rather prints [].所以反向方法应该打印出[5,4,3,2,1],而是打印[]。 I think it has something to do with passing the reversedList between the if and else statement but I'm not sure.我认为这与在 if 和 else 语句之间传递 reversedList 有关,但我不确定。

You are printing the original list lst only which is now empty as program has removed all the elements.您正在打印原始列表lst ,该列表现在为空,因为程序已删除所有元素。

You must reassign the lst to new object reference returned from function like lst = reverse(lst);您必须将lst重新分配给从 function 返回的新 object 引用,例如lst = reverse(lst);

OR you can use System.out.println(reversedList)或者你可以使用System.out.println(reversedList)

    public static void main(String[] args)
    {
        ArrayList<Integer> lst = Recursive.buildList(5);
        System.out.println(lst);
        lst = reverse(lst);                   ////update lst reference

        System.out.println(lst);
        System.out.println(reversedList);    //// OR print reversedList directly 
    }

Hope you find it helpful.希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想检查堆栈集合中的每个元素并反向打印。 打印输出应该打印每个元素,只要它按升序排列 - I want check each element in the stack collection and print it in reverse. the print out should print each element as long as its in ascending order 如何创建在反向打印数组的 Main 方法中调用的 Print 方法? - How do I create a Print Method that is called in the Main method that prints an array in reverse? 没有反向方法如何反向ArrayList? - How to reverse an ArrayList without the reverse method? 如何使用reverse方法反转ArrayList的所有元素? - How to reverse the all element of ArrayList using reverse method? 我想通过将 arraylist 传递给 java 中的新堆栈来反向打印 arrayList - I want to print the arrayList in reverse by passing the arraylist to a new stack in java 为什么不能两次调用 scanEverything 方法? 它只打印一次 arrayList,第二个 println 显示一个空列表 - Why is it not possible to call the scanEverything method twice? It only prints out the arrayList once, and the second println shows an empty list 输入指令并使用ArrayList以相反的顺序打印出来 - Input instructions and print them out in reverse order using ArrayList 如何从控制台将字符串列表添加到arraylist中,并且程序应在按ctrl + z时显示字符串的反写 - how to add list of strings into an arraylist from console and the program should print the reverse of string on pressing ctrl+z 打印给定 ArrayList 反转的 Java 代码 - Java Code That Prints Reverse of Given ArrayList 当我打印出我的列表时,它会为我的所有值打印出 null..... 我该如何解决这个问题???? 我想学习如何解决这个问题以备将来使用 - When I print out my list it prints out null for all my values..... How do I fix this issue???? I want to learn how to fix this issue for future
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM