当前位置: 首页 > 知识库问答 >
问题:

Java构造函数:实际参数列表和正式参数列表的长度不同

武成和
2023-03-14

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

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

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;
    }


}

共有1个答案

段干华晖
2023-03-14

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

BinaryNode (){
    ...
}

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

我希望这对你有帮助。

 类似资料:
  • 我一直在拼命地尝试让这段代码工作。这是我第一次使用对象/构造函数(我是一个新程序员),这让我很困惑。我完全诚实;我不知道问题是什么。我已经尝试了很多东西,所以这段代码相当混乱。我也受够了,重新开始制作第二个代码,它也因为与第一个代码相同的原因而不起作用,尽管该代码的结构略有不同。我将在下面发布这两次尝试,希望有人能看到我的错误。谢谢! 尝试1: 尝试2: 任何帮助都将不胜感激。此外,因为我认为我的

  • 我不明白为什么它会给我一个错误代码,找不到合适的构造函数。 怎么了?

  • 我正在用Java编写一个Volume and Book类,以帮助我更好地理解构造函数和对象——基本上是OOP的更广泛方面。当我尝试创建一个主类时,我收到一个错误,其中说明了以下内容: “类卷中的构造函数卷不能应用于给定类型;必需:字符串、整数、Book[] 找到:无参数 原因:实际列表和正式列表的长度不同 ----” 这是我目前掌握的代码。 一、卷类: } 这里是main,我收到了上面提到的错误:

  • 当我试图在的()括号中放入一些内容时,出现了错误: 类Friends中的构造函数Friends不能应用于给定类型。必填:无参数。Found:字符串,int.原因:实际的或正式的参数列表的长度不同。 但是当我取出参数时,我的朋友列表只显示“null 0”。即使我有也没有设置这些值吗? 而且,当我试图删除一个朋友,它没有任何作用。在源代码中,它确实会提出一个警告, 对util.java.Collect

  • 当我试图在<code>Friends f=new Friend(friendsName,Friends Age)的()括号中添加内容时 出现错误: 朋友类中的构造函数朋友不能应用于给定类型。必需:无参数。找到:字符串,int。原因:实际或正式参数列表的长度不同。 但当我取出参数时,我的朋友列表只显示“空0”。即使我有

  • 我开始学习Java,遇到了一个我无法解决的问题。我有一个名为MyClass的类,带有构造函数。我想将该构造函数设置为访问私有字段: 当我删除构造函数时,我可以从另一个类调用somethingElse。然而,当我沿着这条路线尝试一些东西时 我在data=new MyClass()处得到一个错误,即实际参数和形式参数的长度不同,并且“需要long,found no参数”。我该如何解决这个问题?