如何防止我的索引在java的递归函数中重置?

如何防止我的索引在java的递归函数中重置?,java,indexing,Java,Indexing,我试图将我的二叉树插入数组,函数中有一个递归循环,当它重新启动我的索引get reset时,我在数组中以错误的顺序输入值。 该指数基本上已日化 public boolean toArray (Node _root, String[] arr, int i) { if (_root == null) { return false; } if (_root.isleaf()) { arr[i]=this.data; i++; if (this.father == null)

我试图将我的二叉树插入数组,函数中有一个递归循环,当它重新启动我的索引get reset时,我在数组中以错误的顺序输入值。 该指数基本上已日化

public boolean  toArray (Node _root, String[] arr, int i)
{
if (_root == null)
{
return false;
}
if (_root.isleaf())
{
    arr[i]=this.data;
    i++;
    if (this.father == null)
        return false;
    else if (this.father.ls==this)
    {
        this.father.ls = null;
        return false;
    }
    else 
    {
        this.father.rs=null;
        return true;
    }
}
    if (_root.ls ==null)
        {
        arr[i] = _root.data;
        i++;
        _root.data=null;
        }
    if ( _root.ls!=null && _root.ls.isleaf() )
    {
        arr[i] = _root.ls.data;
        i++;
        _root.ls = null;
        arr[i]=_root.data;
        i++;

    }
    if ( _root.rs!=null && _root.rs.isleaf())
    {
        arr[i]=_root.rs.data;
        i++;
        _root.rs = null;
    }
    toArray(_root.ls, arr,i);
    toArray(_root.rs, arr,i);
    if (this.data !=null)
        {
        arr[i] = this.data;
        i++;
        this.data = null;
        _root.data=null;
        }
    else 
        return false;
    return false;
}
这是我的主课

public class Test_Tree {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    BT mytree = new BT();
    mytree.in("11","","","");
    mytree.in("2","","","");
    mytree.in("810","","","");
    mytree.in("17","","","");
    mytree.in("845","","","");
    mytree.in("10","","","");
    mytree.in("1","","","");
    String[] arr = new String[10];
    Node myroot = new Node (mytree._root);
    int i=0;
    myroot.toArray(myroot, arr,i);
    for (int j=0; j<arr.length;j++)
    {
        System.out.println(arr[j]);
    }
    }

}
公共类测试树{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
BT mytree=新的BT();
mytree.in(“11”,“”,“”,“”,“”);
mytree.in(“2”,“”,“”,“”,“”);
mytree.in(“810”、“”、“”、“”、“”);
mytree.in(“17”、“17”、“17”、“17”);
mytree.in(“845”、“”、“”、“”、“”);
mytree.in(“10”、“”、“”、“”);
mytree.in(“1”,“”,“”,“”,“”);
字符串[]arr=新字符串[10];
Node myroot=新节点(mytree.\u root);
int i=0;
myroot.toArray(myroot,arr,i);

对于(int j=0;j您遇到的问题是,在执行第一个递归调用
toArray(_root.ls,arr,i)之后;
,则
i
的值不会更改,但将保持不变。这是因为在递归中通过值传递整数,然后在调用返回后,本地
i
将不会更改。解决此问题的一种方法是,当函数r您似乎使用布尔返回值来指示给定子树是否为空。我相信您将不再需要它

代码类似于:

public boolean  toArray (Node _root, String[] arr, int i)
{
  if (_root == null)
  {
    return i;
  }
  if (_root.isleaf())
  {
    arr[i]=this.data;
    i++;
    if (this.father == null)
        return i;
    else if (this.father.ls==this)
    {
        this.father.ls = null;
        return i;
    }
    else 
    {
        this.father.rs=null;
        return i;
    }
  }
  if (_root.ls ==null)
  {
    arr[i] = _root.data;
    i++;
    _root.data=null;
  }
  if ( _root.ls!=null && _root.ls.isleaf() )
  {
    arr[i] = _root.ls.data;
    i++;
    _root.ls = null;
    arr[i]=_root.data;
    i++;
  }
  if ( _root.rs!=null && _root.rs.isleaf())
  {
    arr[i]=_root.rs.data;
    i++;
    _root.rs = null;
  }
  i = toArray(_root.ls, arr,i);
  i = toArray(_root.rs, arr,i);
  if (this.data !=null)
  {
    arr[i] = this.data;
    i++;
    this.data = null;
    _root.data=null;
  }
  return i;
}

请正确格式化您的代码,然后给出示例输入预期输出和实际输出。否则将很难帮助您。添加到@Ivaylo-提供类似于javadoc的内容将有所帮助。这是对1的描述。方法将做什么,2.输入是什么,3.返回值I向二叉树插入数字,Iaxpect按以下顺序打印它们-1、2、10、11、17和co,但我的索引被重置,函数将值17插入数组的第一个单元格