Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java int/String链表和变量的问题_Java_Linked List_Circular Reference - Fatal编程技术网

Java int/String链表和变量的问题

Java int/String链表和变量的问题,java,linked-list,circular-reference,Java,Linked List,Circular Reference,我在用Java创建链表时遇到了一些问题。我下面的所有指南都给出了使用字符串类型变量的示例,但我必须创建的列表需要int类型。当我尝试调用position.link之类的函数时,使用int类型会产生错误消息,因为它表示无法将int转换为字符串 为了清楚起见,主程序应该让Scanner请求int,并使用该int创建创建每个节点的循环。我把迭代器和简单的单数链表弄得一团糟,但我什么也没得到 import java.util.NoSuchElementException; public class Su

我在用Java创建链表时遇到了一些问题。我下面的所有指南都给出了使用字符串类型变量的示例,但我必须创建的列表需要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();
   }
}
我已尝试创建常规链接列表,并取得了以下成果:

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()
}
<>但是,当我这样做时,变量链接、位置和头总是说它们没有初始化,除非我把它们放在方法中(如果我在列表中间调用这个方法,它会弄乱我的代码)。
我的问题归结为1)如何将整数转换为字符串以使用链表?2)为什么程序中的变量SuiterList要求我在每个实例中重新初始化它们,而我已经尝试将它们放在任何地方?

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

public String peak()
{
    if(!hasNext())
        throw new IllegalStateException();
    return position.suitor;
}
它被定义为返回一个
字符串

相反,它应该定义为
suiter
的类型,即
int

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

public String next()
相同的问题应该是
public int next()

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

public String peak()
{
    if(!hasNext())
        throw new IllegalStateException();
    return position.suitor;
}
它被定义为返回一个
字符串

相反,它应该定义为
suiter
的类型,即
int

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

public String next()
相同的问题应该是
public int next()

变量suiter的变量类型为int。可以将其转换为字符串。通过转换它

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

这个方法应该可以消除这个错误。或者使用
String.valueOf(position.suiter)
变量suiter的变量类型为int。可以将其转换为字符串。通过转换它

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

这个方法应该可以消除这个错误。或者使用
String.valueOf(position.suiter)

我对链表的理解非常松散,我无法理解我遇到的大多数示例。如果您将带有错误的方法签名从
public String peak()
更改为
public int peak()
,因为您希望它返回
int
,会发生什么?你可能也想对
next()
的签名和
next()
中的
toReturn
变量的声明做同样的事情。我相信这是我的问题,我只需要确保这是我完成主程序时唯一的问题。我对链表的理解非常松散,我还没能理解我遇到的大多数示例。如果您将带有错误的方法签名从
public String peak()
更改为
public int peak()
,因为您希望它返回
int
,会发生什么?您可能想对
next()
的签名和
next()
中的
toReturn
变量的声明执行相同的操作。我相信这是我的问题,我只需确保在完成主程序时这是唯一的一个。谢谢,我知道阅读那些只使用字符串的指南最终会把我搞得一团糟。如果我的主要方法显示出类似的问题,我会给出任何更新。谢谢,我知道阅读仅使用字符串的指南最终会把我搞砸。如果我的主要方法出现类似的问题,我会给出任何更新。