【问题标题】:Java Constructors: Actual and formal argument lists differ in lengthJava 构造函数:实际参数列表和形式参数列表的长度不同
【发布时间】:2016-04-04 05:04:17
【问题描述】:

所以我得到了一个任务,我必须在一个有点大而草率的代码中找到并修复许多错误。我正在寻找看起来是最后一个的东西,但我找不到解决这个问题的方法。我读过类似的场景,人们会犯同样的错误,但我无法将它们与我的代码联系起来。这是我得到错误的地方:Temp = new BinaryNode(AId,AValue);

类 BinaryNode 中的构造函数 BinaryNode 不能应用于给定 类型;

Btree 类

package evidencia2datos;


public class BTree {
private BinaryNode Root;
    private int NoOfNodes;

    private BTree()
      {                                      
      Root = null;
      NoOfNodes = 0;
}
    //operaciones
    public boolean IsEmpty() //busca valor en NoOfNodes
      {
      return(NoOfNodes == 0);
      }

    public BinaryNode gRoot()
      {
      return Root;
      }

    public int Count() //valor de NoOfNodes
      {
      return NoOfNodes;
      }

    //size del arbol
     public int Size(BinaryNode ATree)
      {
      if (ATree == null)
        return 0;
      else
        return(1 + Size(ATree.gLeft()) + Size(ATree.gRight()));
      }

     //niveles
    public int Height(BinaryNode ATree)
      {
      if (ATree == null)
        return 0;
      else
        return (1 + Math.max(Height(ATree.gLeft()), Height(ATree.gRight())));
      }

    //traversales
    public void PreOrder(BinaryNode ATree)
      {
      if (ATree != null)
        {
        System.out.println(ATree.gData());
        PreOrder(ATree.gLeft());
        PreOrder(ATree.gRight());
        }
      }

    public void InOrder(BinaryNode ATree)
      {
      if (ATree != null)
        {
        InOrder(ATree.gLeft());
        System.out.println(ATree.gData());
        InOrder(ATree.gRight());
        }
      }

public void PostOrder(BinaryNode ATree)
      {
      if (ATree != null)
        {
        PostOrder(ATree.gLeft());
        PostOrder(ATree.gRight());
        System.out.println(ATree.gData());
        }
      }

//insertar valores
  public void Insert(int AId, Object AValue)
      {



      BinaryNode Temp,Current,Parent;
      if(Root == null)//tree is empty
        {
        Temp = new BinaryNode(AId,AValue);
        Root = Temp;
        NoOfNodes++;
        }
      else//tree is not empty
        {
        Temp = new BinaryNode(AId,AValue);
        Current = Root;
        while(true)//never ending while loop
          {
          Parent = Current;
          if(AId < Current.gKey())
            {//go left
            Current = Current.gLeft();
              if (Current == null)
                {
                Parent.sLeft(Temp);
                NoOfNodes++;
                return;//jump out of loop
                }
            }
          else
            { //go right
            Current = Current.gRight();
            if(Current == null)
              {
              Parent.sRight(Temp);
              NoOfNodes++;
              return;
              }
            }
          }
        }

  }

  //search
    public BinaryNode Find(int AKey)
      {
      BinaryNode Current = null;
      if(!IsEmpty())
        {
        Current = Root; //start search at top of tree
        while(Current.gKey() != AKey)
          {
          if(AKey < Current.gKey())
            Current = Current.gLeft();
          else
            Current = Current.gRight();
          if(Current == null)
            return null;
          }
        }
        return Current;
      }

    //succesor

   public BinaryNode GetSuccessor(BinaryNode ANode)
     {
     BinaryNode Current,Successor,SuccessorParent;
     Successor = ANode;
     SuccessorParent = ANode;
     Current = ANode.gRight();
     while(Current !=null)
       {
       SuccessorParent = Successor;
       Successor = Current;
       Current = Current.gLeft();
       }
     if(Successor != ANode.gRight())
       {
       SuccessorParent.sLeft(Successor.gRight());
       Successor.sRight(ANode.gRight());
       }
     return Successor;
     }

      public boolean Delete (int AKey)
     {
     BinaryNode Current, Parent;
     boolean IsLeftChild = true;
     Current = Root;
     Parent = Root;
     while (Current.gKey() != AKey)
       {
       Parent = Current;
       if (AKey < Current.gKey())
         {
         IsLeftChild = true;
         Current = Current.gLeft();
}
      else
         {
         IsLeftChild = false;
         Current = Current.gRight();
         }
       if(Current == null)
         return false;
       }
      // if no children delete the node
      if (Current.gLeft() == null && Current.gRight() == null)
        {
        if (Current == Root)
          Root = Current.gLeft();
        else
          if (IsLeftChild)
            Parent.sLeft(Current.gRight());
          else
            Parent.sRight(Current.gRight());
        }
      // if no right child replace with left subtree
      else
        {
        if (Current.gRight() == null)
          {
          if (Current == Root)
   Root = Current.gRight();
          else
            if (IsLeftChild)
              Parent.sLeft(Current.gLeft());
            else
              Parent.sRight(Current.gLeft());
          }
        // if no left child replace with right subtree
        else
          {
          if (Current.gLeft() == null)
            {
            if (Current == Root)
              Root = Current.gLeft();
            else
              if (IsLeftChild)
                Parent.sLeft(Current.gRight());
              else
               Parent.sRight(Current.gRight());
            }
          // two children so replace in order of successor
          else
            {
            BinaryNode Successor = GetSuccessor(Current);
            if (Current == Root)
              Root = Successor;
            else
              if (IsLeftChild)
                Parent.sLeft(Successor);
              else
                Parent.sRight(Successor);
            Successor.sLeft(Current.gLeft());
            }
          }
        }
     NoOfNodes--;
     return true;
     }

    public static void main(String[] args) {
    BTree MyTree = new BTree();                
    BinaryNode NodeAt;                           

    MyTree.Insert(12,"Jorge");
    MyTree.Insert(4,"Andres");
    MyTree.Insert(11,"Javier");
    MyTree.Insert(1,"Jose");
    MyTree.Insert(100,"Paty");
    MyTree.Delete(1);
    MyTree.InOrder(MyTree.gRoot());       
    NodeAt = MyTree.Find(11);

    if(NodeAt !=null)
      System.out.println("Data in Node with Key 11 = " + NodeAt.gData());
    System.exit(0);  
    }

}

BinaryNode 类

package evidencia2datos;

public class BinaryNode {
  private int Key;
  private Object Data;
  private BinaryNode Left;
  private BinaryNode Right;

  public BinaryNode()
    { 
    java.util.Scanner scaniar = new java.util.Scanner(System.in);
    System.out.print("Enter in Key Value: ");
    Key = scaniar.nextInt();
    System.out.print("Enter in data: ");
    Data = scaniar.nextInt();
    Left = null;
    Right = null;
}


   //get
  public int gKey()
    {
    return Key;
    }

  public Object gData()
    {
    return Data;
    }

  public BinaryNode gLeft()
    {
    return Left;
    }
  public BinaryNode gRight()
    {
    return Right;
    }

  //set
    public void sKey(int AValue)
    {
    Key = AValue;
    }

  public void sData(Object AValue)
    {
    Data = AValue;
    }

  public void sLeft( BinaryNode AValue)
    {
    Left = AValue;
    }

  public void sRight( BinaryNode AValue)
    {
    Right = AValue;
    }


}

【问题讨论】:

  • 您正在尝试使用不存在的 BinaryNode 的构造函数签名。
  • 谢谢,我能再多输入一点吗?我还是很困惑。我看到在 BinaryNode 类中它不是构造函数而是方法。我可以进行哪些更改以保持代码正常工作?我尝试使该方法成为具有各自格式的构造函数,但在包含的所有参数上都出现了无数错误。
  • 什么不是构造函数而是方法?我的建议是找到这些问题的答案:(1)Insert 在使用带有两个参数的构造函数时试图做什么?查看参数的类型。 (2) 如何解决问题,以便 (a) Insert 使用已经存在的构造函数,或者 (b) 添加一个新的构造函数来执行 Insert 尝试做的事情?

标签: java constructor


【解决方案1】:

您可以创建一个带有两个参数的新 BinaryNode 构造函数

BinaryNode (){
    ...
}

//Este es el nuevo constructor, como se puede observar
//toma dos argumentos
BinaryNode (int k, Object d){
   key = k;
   data = d;
   ...
}

希望对你有帮助。

【讨论】:

  • 成功了,谢谢!虽然现在当我运行程序时,我得到 5 个插入的全部为空。
  • @Dotol 检查当前,树为空时没有任何值
猜你喜欢
  • 2020-08-13
  • 1970-01-01
  • 2014-04-06
  • 2019-08-10
  • 2014-05-13
  • 1970-01-01
  • 2023-03-25
  • 2015-07-25
  • 2013-02-15
相关资源
最近更新 更多