首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为subList编写LinkedList方法

为subList编写LinkedList方法
EN

Stack Overflow用户
提问于 2016-06-27 18:16:41
回答 1查看 1.4K关注 0票数 0

作为作业的一部分,我正在编写一个具有虚拟头节点的单链接、非循环的LinkedList,同时实现泛型。分配需要实现List接口,但是我仍然停留在subList方法上。我一直在浏览StackOverflow和整个网络,试图看到一个例子来说明它是如何完成的,因为我尝试了几种我自己设计的不同方法,但是subList中的变化并没有反映到最初的LinkedList中。我重写了我的方法,试图遵循顶部答案here的结构(在某种程度上,我不需要任何帮助方法),下面是我的代码:

代码语言:javascript
复制
@Override
public List<E> subList(final int fromIndex, final int toIndex){//FIX ME
  if(fromIndex < 0 || fromIndex > this.size()-1 || toIndex < 0 || toIndex > this.size()-1){
     throw new IndexOutOfBoundsException("Index out of bounds on call to subList with fromIndex of" 
     + fromIndex + " and toIndex of" + toIndex);
  }
  List<E> list = new LinkedList<E>();
  Node<E> cur = this.head.next;
  int count = 0;
  while(cur!=null){
     if(count >= fromIndex && count < toIndex){
        list.add(cur.data);
     }
     cur = cur.next;
     count++;
  }

  return list;
}// end sublist

下面是我的测试器文件的一个片段,您可以看到我用正确的节点创建了一个subList,但是在subList中所做的更改并没有反映在原始的LinkedList中,我不知道如何继续修复这个问题:

代码语言:javascript
复制
New LinkedList has been created 
List before testing: [one, two, three, four, five]
Testing subList function with fromIndex of 1, and toIndex of 4
Printing subList: [two, three, four]
Changing data of sublist to 'six, seven, eight'
Printing subList: [six, seven, eight]
Printing LinkedList after test: [one, two, three, four, five]

我不确定使用LinkedList作为我的subList是正确的选择,任何建议或批评都将不胜感激!

编辑:在下面回答我自己的问题时,我实际上是在创建新节点,而不是直接指向原始LinkedList中的节点。

EN

回答 1

Stack Overflow用户

发布于 2016-06-27 22:28:59

我将回答我自己的问题,希望这对任何试图在将来查这件事的人都有用。

因此,在我的代码中,我使用了LinkedList中的add( index,E data)函数,按照我编写它的方式,它创建了一个新节点并将其插入到列表中,而不仅仅是更改数据变量本身。因此,我重写了它,使它不再创建新节点,然后我将上面的subList类更改为:

代码语言:javascript
复制
public List<E> subList(final int fromIndex, final int toIndex){//FIX ME
  if(fromIndex < 0 || fromIndex > this.size()-1 || toIndex < 0 || toIndex > this.size()-1){
     throw new IndexOutOfBoundsException("Index out of bounds on call to subList with fromIndex of" + fromIndex + " and toIndex of" + toIndex);
  }
  LinkedList<E> list = new LinkedList<E>();
  Node<E> cur = this.head.next;
  Node<E> pointer = list.head;
  int count = 0;
  while(cur!=null){
     if(count >= fromIndex && count < toIndex){
        pointer.next = cur;
        list.size++;
        pointer = pointer.next;
     }
     cur = cur.next;
     count++;
  }
  return list;

现在,我没有使用add函数,而是直接将原始LinkedList中的Node分配给subList中的Node,并手动增加大小。

不幸的是,因为每个节点都有一个下一个引用,即使我正确地添加了它们,当调用我的toString函数时,它将遍历整个LinkedList,包括通过我的'toIndex‘的节点,直到下一个节点为null。为了解决这个问题,我在我的toString中添加了一个附加条件,其中包含了大小,并更改了if语句,以便在返回的字符串中添加逗号:

代码语言:javascript
复制
public String toString(){
  String ret = "[";
  Node cur= this.head.next;
  int index = 0;
  while(cur != null && index < size){// added the index < size condition
     ret = ret + cur.data;
     if(index < this.size -1){// changed from cur.next != null
        ret = ret + ", ";
     }
     cur = cur.next;
     index++;
  }

  ret = ret + "]";
  return ret; 

}// end toString

最后,我的测试输出如下所示:

代码语言:javascript
复制
Testing subList function with fromIndex of 1, and toIndex of 4
Printing subList: [two, three, four]
Changing data of sublist to 'six, seven, eight'
Printing subList: [six, seven, eight]
Printing LinkedList after test: [one, six, seven, eight, five]

虽然我还在学习,而且我的代码可能并不理想,但希望这个响应能够帮助其他人在将来解决同样的问题!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38060593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档