Java-带有递归调用的静态ArrayList的问题

Java-带有递归调用的静态ArrayList的问题,java,arraylist,heap,Java,Arraylist,Heap,我编写了以下程序来最大化堆: package heap; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class HeapMaxify { public static List<Integer> inp; public static void main(String[] s) { inp = new ArrayLis

我编写了以下程序来最大化堆:

package heap;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HeapMaxify 
{
    public static List<Integer> inp;
    public static void main(String[] s)
    {
        inp = new ArrayList<Integer>();
        Scanner inpObj = new Scanner(System.in);
        inp.add(inpObj.nextInt());
        for(int i=1;i<=inp.get(0);i++)
            inp.add(inpObj.nextInt());
        System.out.println("Current heap follows :");
        int elemInLine = 1;
        int count = 0;
        for(int i=1;i<=inp.get(0);i++)
        {
            System.out.print(inp.get(i)+"\t");
            count++;
            if(count==elemInLine)
            {
                System.out.println();
                count = 0;
                elemInLine *= 2;
            }
        }
        maxifyHeap(inp,1);
        System.out.println("Maxified heap follows :");
        elemInLine = 1;
        count = 0;
        for(int i=1;i<=inp.get(0);i++)
        {
            System.out.print(inp.get(i)+"\t");
            count++;
            if(count==elemInLine)
            {
                System.out.println();
                count = 0;
                elemInLine *= 2;
            }
        }
    }
    private static void maxifyHeap(List<Integer> inp, int curIndex)
    {
        int leftIndex = 2*curIndex;
        int rightIndex = (2*curIndex)+1;
        int largestIndex=0;
        int temp;
        if(leftIndex<=inp.get(0)&&inp.get(leftIndex)>inp.get(curIndex))
            largestIndex = leftIndex;
        else
            largestIndex = curIndex;
        if(rightIndex<=inp.get(0)&&inp.get(rightIndex)>inp.get(largestIndex))
            largestIndex = rightIndex;
        if(largestIndex!=curIndex)
        {
            temp = inp.get(largestIndex);
            inp.set(largestIndex, inp.get(curIndex));
            inp.set(curIndex, temp);
            maxifyHeap(inp, largestIndex);
        }
    }
}

首先,不清楚为什么要使用静态变量。您已经有了一个接受变量的参数,因此您也可以在
main
中将
inp
设置为局部变量。我也不会仅仅使用第一个元素作为大小;一个列表已经知道它的大小。你应该把这些概念分开。但是当我运行你的代码时,列表确实改变了。。。请给出一个没有的例子。(理想情况下,只需对输入进行硬编码-这将使我们更容易重现问题。)谢谢。你能告诉我在递归方法完成处理后返回arraylist的最佳方法是什么吗?用outputRight更新了问题-改为使用数字1、2、3、4、5。然后你会看到不同。所以,这不是范围的问题,也不是无法修改列表的问题,而是您的
maxifyHeap
方法不正确的问题。啊!知道了。谢谢你的意见。我会再看看我的代码。
10
16
4
10
14
7
9
3
2
8
1
Current heap follows :
16  
4   10  
14  7   9   3   
2   8   1   Maxified heap follows :
16  
4   10  
14  7   9   3   
2   8   1