Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.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 如何在移动所有其他元素的同时将索引元素移动到单个列表的末尾_Java_Singly Linked List - Fatal编程技术网

Java 如何在移动所有其他元素的同时将索引元素移动到单个列表的末尾

Java 如何在移动所有其他元素的同时将索引元素移动到单个列表的末尾,java,singly-linked-list,Java,Singly Linked List,我试图将索引元素移动到单个列表的末尾,同时移动所有其他元素,因此如果我有一个{1,2,3,4,5}列表,并将索引2传递给它,它将返回{1,2,4,5,3} public void moveToLast(int index) throws IllegalArgumentException { if(index<0 || index > size()) throw new IllegalArgumentException("" + index); Node ref

我试图将索引元素移动到单个列表的末尾,同时移动所有其他元素,因此如果我有一个{1,2,3,4,5}列表,并将索引2传递给它,它将返回{1,2,4,5,3}

public void moveToLast(int index) throws IllegalArgumentException {
  if(index<0 || index > size())
        throw new IllegalArgumentException("" + index);
  Node ref= first;
  Node otherRef=first;
  for(int i=0;i<index;i++){
      ref=ref.next;
      for(int j=0;j<size()-1;j++){
          if(otherRef.next!=null)
              otherRef=otherRef.next;
      }   
  }
  E temp=ref.data;
  ref.data=otherRef.data;
  otherRef.data=temp;
public void moveToLast(int index)引发IllegalArgumentException{
if(索引大小())
抛出新的IllegalArgumentException(“+”索引);
节点ref=第一;
节点otherRef=第一个;

对于(int i=0;i,您必须在循环时更新引用,而不是在循环结束时

这是一个小算法,可以节省双循环。我实现了它,交换了引用,而不是
数据
,但结果没有实际差异。我不确定一种方法或另一种方法有什么限制,但如果你有移除节点等的方法,你通常会使用引用,而不是值是的

我没有编译它,所以请原谅任何错误,但我认为类似的东西应该可以工作:

public void moveToLast(int index) throws IllegalArgumentException {
    if(index<0 || index > size())
        throw new IllegalArgumentException("" + index);
    Node refToMove= first;
    Node previousRef=null;
    for(int i=0;i<index && refToMove!=null;i++){
        previousRef=refToMove;
        refToMove=refToMove.next;
    }
    if(refToMove!=null && previousRef!=null) {
           Node nextRef=refToMove.next;
           while(nextRef!=null) {
               previousRef.next = nextRef;
               Node tempRef = nextRef.next;
               nextRef.next = refToMove;
               refToMove.next = tempRef;
               nextRef = tempRef;
           }
    }
public void moveToLast(int index)引发IllegalArgumentException{
if(索引大小())
抛出新的IllegalArgumentException(“+”索引);
节点refToMove=first;
节点previousRef=null;

对于(int i=0;i,您必须在循环时更新引用,而不是在循环结束时

这是一个小算法,可以节省双循环。我实现了它,交换了引用,而不是
数据
,但结果没有实际差异。我不确定一种方法或另一种方法有什么限制,但如果你有移除节点等的方法,你通常会使用引用,而不是值是的

我没有编译它,所以请原谅任何错误,但我认为类似的东西应该可以工作:

public void moveToLast(int index) throws IllegalArgumentException {
    if(index<0 || index > size())
        throw new IllegalArgumentException("" + index);
    Node refToMove= first;
    Node previousRef=null;
    for(int i=0;i<index && refToMove!=null;i++){
        previousRef=refToMove;
        refToMove=refToMove.next;
    }
    if(refToMove!=null && previousRef!=null) {
           Node nextRef=refToMove.next;
           while(nextRef!=null) {
               previousRef.next = nextRef;
               Node tempRef = nextRef.next;
               nextRef.next = refToMove;
               refToMove.next = tempRef;
               nextRef = tempRef;
           }
    }
public void moveToLast(int index)引发IllegalArgumentException{
if(索引大小())
抛出新的IllegalArgumentException(“+”索引);
节点refToMove=first;
节点previousRef=null;
对于(inti=0;i也考虑这一点

if(index<0 || index > size())
就像您所说的,您的代码只在找到索引元素end last元素并在末尾分配它们时交换

您需要做的是找到第一个元素,然后为每个集合找到指向下一个元素的链接,直到最后一个元素,然后像这样设置最后一个元素来链接原始索引

public void moveToLast(int index) throws IllegalArgumentException {
    if(index<0 || index >= size())
       throw new IllegalArgumentException("" + index);
    if( index == (size()-1))
       return
    Node currentElement = first;
    Node elementToMove;
    Node nextElement;
    // if first is the one to move
    if(index == 0)
    { 
        elementToMove = first;
        currentElement = first.next;
        first = currentElement
    }
    // Iterate over the list only once
    for(int i = 0; i < size()-2; i++)  // up to the 2nd to last element as the next element is referenced
    {
         nextElement = currentElement.next
         if(i+1 == index) // next element is the one you want to move
         {
              // store reference to the element you want to move
              elementToMove = nextElement;
              // Skip this element and update to the next one
              nextElement = nextElement.next;
         }
         // Update reference
         currentElement.next = nextElement;
         // Move to next element
         currentElement = nextElement
    }
    // Put the one to move on the end
    currentElement.next = elementToMove
}
public void moveToLast(int index)引发IllegalArgumentException{
如果(索引=大小())
抛出新的IllegalArgumentException(“+”索引);
如果(索引==(大小()-1))
返回
节点currentElement=第一个;
节点元素移动;
节点连接;
//如果第一个是要移动的
如果(索引==0)
{ 
elementToMove=第一;
currentElement=first.next;
第一个=当前元素
}
//仅在列表上迭代一次
for(int i=0;i
这节省了您在列表上循环两次的时间,我没有检查或遵守这一点,因此您可能需要在此处添加空检查。如果输入的索引为0,您也需要特别考虑,即您需要先重置

,也要考虑这一点

if(index<0 || index > size())
就像您所说的,您的代码只在找到索引元素end last元素并在末尾分配它们时交换

您需要做的是找到第一个元素,然后为每个集合找到指向下一个元素的链接,直到最后一个元素,然后像这样设置最后一个元素来链接原始索引

public void moveToLast(int index) throws IllegalArgumentException {
    if(index<0 || index >= size())
       throw new IllegalArgumentException("" + index);
    if( index == (size()-1))
       return
    Node currentElement = first;
    Node elementToMove;
    Node nextElement;
    // if first is the one to move
    if(index == 0)
    { 
        elementToMove = first;
        currentElement = first.next;
        first = currentElement
    }
    // Iterate over the list only once
    for(int i = 0; i < size()-2; i++)  // up to the 2nd to last element as the next element is referenced
    {
         nextElement = currentElement.next
         if(i+1 == index) // next element is the one you want to move
         {
              // store reference to the element you want to move
              elementToMove = nextElement;
              // Skip this element and update to the next one
              nextElement = nextElement.next;
         }
         // Update reference
         currentElement.next = nextElement;
         // Move to next element
         currentElement = nextElement
    }
    // Put the one to move on the end
    currentElement.next = elementToMove
}
public void moveToLast(int index)引发IllegalArgumentException{
如果(索引=大小())
抛出新的IllegalArgumentException(“+”索引);
如果(索引==(大小()-1))
返回
节点currentElement=第一个;
节点元素移动;
节点连接;
//如果第一个是要移动的
如果(索引==0)
{ 
elementToMove=第一;
currentElement=first.next;
第一个=当前元素
}
//仅在列表上迭代一次
for(int i=0;i

这节省了您在列表上循环两次的时间,我没有检查或遵守这一点,因此您可能需要在此处添加空检查。如果输入的索引为0,您也需要特别考虑,即您需要先重置

,您可能应该告诉op他们做错了什么,或者这一项工作的原因。感谢您的帮助代码,但我实现了它,它没有正确地移动值,然后在另一个测试中出现空指针异常。我编辑了代码,我实现了它,考虑了一个双链接列表,但事实并非如此,抱歉。您可能应该告诉op他们做错了什么,或者为什么这个可以工作。感谢您提供的代码,但我实现了它,并且没有问题未正确移动值,并且