java - int/String 链表和变量的问题

标签 java linked-list circular-reference

我在 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 放置在任何可以放置的地方时,为什么要求我在每个实例中重新初始化它们?

最佳答案

问题是您的 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()

关于java - int/String 链表和变量的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47356888/

相关文章:

java - Eclipse 组织导入 : how to prefer JRE classes?

java - 包含对实现代码的引用的 OSGi 导出 API 安全吗?

c - 如何在循环链表中添加节点?

database - 循环引用数据库的异常(exception) - 这是一个吗?

java - 在 Weblogic Server 中查看已部署的应用程序

c - 链表 SortedInsert() 函数

c - 指向结构体的指针数组

javascript - Chrome/V8 不垃圾回收循环引用?

具有聚合和组合的 UML 循环引用

java - Adapter.notifyDataSetChanged() 在 onCreate() 内部不工作