【问题标题】:Problems with int/String linked lists and variablesint/String 链表和变量的问题
【发布时间】:2017-11-17 18:17:00
【问题描述】:

我在 Java 中创建链表时遇到了一些问题。我遵循的所有指南都给出了使用某种 String 类型变量的示例,但我必须创建的列表需要一个 int 类型。当我尝试调用诸如 position.link 之类的东西时,使用 int 类型会产生错误消息,因为它说它不能将 int 转换为字符串。

为了清楚起见,主程序应该让 Scanner 请求 int,并使用该 int 创建一个循环来创建每个节点。我已经搞砸了迭代器和简单的单数链表,但我没有得到任何结果。

import java.util.NoSuchElementException;
public class SuitorLinkedList<Integer>
{
   private class SuitorListNode
   {
      private int suitor;
      private SuitorListNode link;

      public SuitorListNode()
      {
         suitor = 0;
         link = null;
      }

      public SuitorListNode(int newSuitor, SuitorListNode linkValue)
      {
         suitor = newSuitor;
         link = linkValue;
      }
   } // End of SuitorListNode inner class

   public class SuitorListIterator
   {
      public SuitorListNode position;
      private SuitorListNode previous; // previous value of position

      public SuitorListIterator()
      {
         position = head; // variable head of outer class
         previous = null;
      }

      public void restart()
      {
         position = head;
         previous = null;
      }

      public String next()
      {
         if(!hasNext())
            throw new NoSuchElementException();

         String toReturn = position.suitor;
         previous = position;
         position = position.link;
         return toReturn;
      }

      public boolean hasNext()
      {
         return (position != null); // Throws IllegalStateExpression if false
      } // Returns next value to be returned by next()

      public String peak()
      {
         if(!hasNext())
            throw new IllegalStateException();
         return position.suitor;
      }

      public void addHere(int newData)
      {
         if(position == null && previous != null) // At end of list, add to end
            previous.link = new SuitorListNode(newData, null);
         else if(position == null || previous == null) // List empty or position is head node
            head = new SuitorListNode(newData, head);
         else // previous and position are consecutive nodes
         {
            SuitorListNode temp = new SuitorListNode(newData, position);
            previous.link = temp;
            previous = temp;
         }
      }

      public void delete()
      {
         if(position == null)
            throw new IllegalStateException();
         else if (previous == null) // remove node at head
         {
            head = head.link;
            position = head;
         }
         else // previous and position are consecutive nodes
         {
            previous.link = position.link;
            position = position.link;
         }
      }

      private SuitorListNode head;
   }
   public SuitorListIterator iterator()
   {
      return new SuitorListIterator();
   }
}

我每次尝试都会收到此错误,我已尝试搜索并使用 toString() 来提供帮助,但它不起作用:

SuitorLinkedList.java:60: error: incompatible types: int cannot be converted to String
         return position.suitor;
                        ^

我已经尝试创建常规链表并做到了这一点:

public class SuitorList
{
   public class SuitorNode
   {
      public int suitor;
      public SuitorNode link;

      public SuitorNode()
      {
         suitor = 0;
         link = null;
      } // Initialize veriables

      public SuitorNode(int newSuitor, SuitorNode linkValue)
      {
         suitor = newSuitor;
         link = linkValue;
      } // Assigns values sent in from main
   } // End inner class

   private SuitorNode head; // Variable head of type SuitorNode (callback to Node program)
   // Allows head to point to a node

   public SuitorList()
   {
      head = null;
   } // Initialize variables
   // Memory space called head filled with null

   public void addToStart(int suitorNum)
   {
      head = new SuitorNode(suitorNum, head);
   } 

   // Creates node with head pointing to it at start of list
   // head will have a definition as an object with a suitor and link = head
   // If head = null, then link = null
   // head is repositioned to point to node

   public int size() // Reads size of list
   {
      int count = 0;
      SuitorNode position = head; // Variable position of type SuitorNode will equal value at head; position points where head is pointing
      while(position != null) // While list is not empty/ended
      {
         count++; // increase number of entries detected
         position = position.link; // getLink will make position = link, leading to next entry in list
      }
      return count; // Display size.
   }

   public void outputList()
   {
      SuitorNode position = head; // Position points to same thing head points to

      while(position != null) // While list is not empty/ended
      {
         System.out.println(position.suitor); // Print suitor
         position = position.link; // Go to next entry
      }
   }

   public void deleteNode(int count)
   {
      int moveCount = count - 1;
      SuitorNode position = head;

      while(head != link) // not winning
      {
         moveCount = count;
         checkEnd(); // Checks for win before causing potential problem with 1 suitor left
         checkTwoNumbersLeft(moveCount); // Takes care of movement when two nodes are left
         checkEndNode(moveCount); // Checks when, for example, 2 nodes away

         if(moveCount == count) // If checkEndNode and checkTwoNumbersLeft fail
         {
            position = position.link; // Move for first time
            moveCount = moveCount - 1;
         }

         checkEnd();
         checkEndNode2(moveCount); // When one movement is made already, deletes end node after

         if(moveCount == moveCount - 1) // if checkEndNode2 fails
            position = position.link.link; // 2nd deletion
         count = moveCount;
      }

      isWinner();
   } // End method deleteNode()

   public void checkTwoNumbersLeft(int moveCount)
   {
      SuitorNode position;
      if(position.link.link == null) // example: 1 5
      {
         createLoop();
         position = position.link.link; // Deletes the 5
         moveCount = moveCount - 2;
      } // Used just in case only two numbers are present
   } // End method checkTwoNumbersLeft()

   public void checkEnd()
   {
      SuitorNode position;
      if(position.link == null) // If at end of list
      {
         createLoop(); // creates a loop if the initial number has no next value
         isWinner(); // If a 1 is used, the entire if statement will trigger
      } // if true, head == link which will fall out of while in deleteNode()
   } // End method checkEnd()

   public void isWinner()
   {
      SuitorNode link;
      SuitorNode position;
      if(position == position.link)
      {
         head = link;
         System.out.println("The winner is Suitor " + position + "!");
      } 
   } // End method isWinner()

   public void checkEndNode2(int moveCount)
   {
      SuitorNode position;
      SuitorNode link;

      if(position.link.link == null) // 1 movement
      {
         position.link = null;
         createLoop();
         isWinner();
         moveCount = moveCount - 1;
      }
   } // End checkEndNode2()

   public void checkEndNode(int moveCount)
   {
      SuitorNode position;
      SuitorNode link;

      if(position.link.link.link == null) // no movements
      {
         position = position.link;
         position.link = null;
         createLoop();
         isWinner();
         moveCount = moveCount - 2;
      }
   } // End checkEndNode()

   public void createLoop()
   {
      SuitorNode position;
      SuitorNode link;

      if(link == null) // if at the end of the list
         link = head; // Sets link to point to where head points, AKA beginning of list
   } // End createLoop()
}

但是当我这样做时,变量 linkpositionhead 总是说它们没有被初始化,除非我把它们放在方法(如果我在列表中间调用该方法,可能会弄乱我的代码)。

我的问题归结为 1) 我如何能够将整数转换为字符串以便使用链表?和 2) 为什么程序 SuitorList 中的变量在我尝试将它们放置在任何可能的地方时都要求我在每个实例中重新初始化它们?

【问题讨论】:

  • 我对链表的理解非常松散,而且我无法理解我遇到的大多数示例。
  • 如果您将出现错误的方法的签名从public String peak() 更改为public int peak(),因为您希望它返回int,会发生什么情况?您可能希望对next() 的签名和toReturn 变量在next() 中的声明做同样的事情。
  • 我相信这是我的问题,当我完成我的主程序时,我只需要确保这是唯一的问题。

标签: java linked-list circular-reference


【解决方案1】:

问题是你的 peek 函数被定义为错误的类型

public String peak()
{
    if(!hasNext())
        throw new IllegalStateException();
    return position.suitor;
}

定义为返回一个String

相反,它应该被定义为suitor的类型,int

public int peak()
{
    if(!hasNext())
        throw new IllegalStateException();
    return position.suitor;
}

public String next() 相同的问题,应改为public int next()

【讨论】:

  • 谢谢,我知道从只使用字符串的指南中阅读最终会搞砸我。如果我的主要方法显示任何此类问题,我将提供任何更新。
【解决方案2】:

变量suitor被赋予变量类型int。可以它变成一个字符串。通过转换它;

public String peak() { if(!hasNext()) throw new IllegalStateException(); return Integer.toString(position.suitor); }

此方法应该可以消除该错误。或者使用String.valueOf(position.suitor);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多