Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使变量成为局部变量_Java - Fatal编程技术网

Java 如何使变量成为局部变量

Java 如何使变量成为局部变量,java,Java,我制作了这个递归方法,它计算二叉树中的最长路径。该路径将其存储在arralist中,然后返回。但是,我必须声明数组列表变量为全局变量。是否可以使用此方法,但数组列表变量是局部变量 public static <T> ArrayList<T> longestPath(BinaryNode<T> root){ //ArrayList path = new ArrayList(); if(root == null) return null;

我制作了这个递归方法,它计算二叉树中的最长路径。该路径将其存储在arralist中,然后返回。但是,我必须声明数组列表变量为全局变量。是否可以使用此方法,但数组列表变量是局部变量

public static <T> ArrayList<T> longestPath(BinaryNode<T> root){
    //ArrayList path = new ArrayList();

   if(root == null) return null;

   if(height(root.left) > height(root.right)){
       path.add(root.element);

       longestPath(root.left);


   }else{
       path.add(root.element);

       longestPath(root.right);

   }

   return path;

}
publicstaticarraylistlongestpath(binarynoderoot){
//ArrayList路径=新建ArrayList();
if(root==null)返回null;
如果(高度(根部左侧)>高度(根部右侧)){
add(root.element);
最长路径(根。左);
}否则{
add(root.element);
最长路径(根,右);
}
返回路径;
}

我之所以将其设置为全局,是因为它是一个递归程序,每次调用它自己时,它都会创建一个新的数组列表对象变量,地址不同,如果你知道我的意思的话。

在方法参数中传递arraylist:

public static <T> List<T> longestPath(BinaryNode<T> root, List<T> path){

当您最初调用该方法时,只需传递一个
new Arraylist()
如果您需要访问该变量并且无法使其成为全局变量,则另一个选项是将其作为参数传递:

    public static <T> ArrayList<T> longestPath(BinaryNode<T> root, ArrayList path) {
    //...
公共静态ArrayList longestPath(二进制节点根,ArrayList路径){
//...

如果将ArrayList传递给递归函数yes:

public static <T> ArrayList<T> longestPath(BinaryNode<T> root, ArrayList path){
ArrayList lPath = path;

 if(root == null) return null;

 if(height(root.left) > height(root.right)){
   lPath.add(root.element);

   longestPath(root.left, lPath);


 }else{
   lPath.add(root.element);

   longestPath(root.right, lPath);

 }

 return lPath;

 } 
公共静态ArrayList longestPath(二进制节点根,ArrayList路径){
ArrayList lPath=路径;
if(root==null)返回null;
如果(高度(根部左侧)>高度(根部右侧)){
lPath.add(root.element);
最长路径(root.left,lPath);
}否则{
lPath.add(root.element);
最长路径(root.right,lPath);
}
返回lPath;
} 

您应该做的是让主函数创建一个
数组列表
,并将其传递给一个完成所有工作的助手函数;例如:

public static ArrayList longestPath(BinaryNode root)
{
    ArrayList path = new ArrayList();
    return longestPathHelper(root, path);
}

private static ArrayList longestPathHelper(BinaryNode root, ArrayList path)
{
    // Existing code, except recursive calls pass path as well
}

Java中没有全局变量…?@RichardJPLeGuen:他几乎肯定是指
静态
字段。
public static ArrayList longestPath(BinaryNode root)
{
    ArrayList path = new ArrayList();
    return longestPathHelper(root, path);
}

private static ArrayList longestPathHelper(BinaryNode root, ArrayList path)
{
    // Existing code, except recursive calls pass path as well
}