链表子列表方法java
我正在尝试编写一个子列表方法,该方法返回一个由当前对象列表组成的列表,包括索引链表子列表方法java,java,linked-list,nodes,sublist,Java,Linked List,Nodes,Sublist,我正在尝试编写一个子列表方法,该方法返回一个由当前对象列表组成的列表,包括索引fromIndex和toIndex之间的列表 例如,如果我有一个由 3579112023 我调用了子列表(0,3),我应该得到一个新的 35799 返回 我正在使用helper方法来帮助编写该方法。我编写此方法的逻辑是,我将head节点指定为indexfrom index处的节点,同时将列表中的最后一个节点指定给index至index处的节点,但没有返回任何内容。感谢您的帮助 编辑:我创建了一个add方法,可以将节点添
fromIndex
和toIndex
之间的列表
例如,如果我有一个由
3579112023
我调用了子列表(0,3)
,我应该得到一个新的
35799
返回
我正在使用helper方法来帮助编写该方法。我编写此方法的逻辑是,我将head节点指定为indexfrom index
处的节点,同时将列表中的最后一个节点指定给index至index
处的节点,但没有返回任何内容。感谢您的帮助
编辑:我创建了一个add方法,可以将节点添加到列表中。我仍在尝试编写自己的子列表方法
EDIT2:我更新了一些代码,可以在底部看到(忽略上面的旧代码)。fromIndex和toIndex包含在新的子列表中
private class Node<N extends Comparable<N>> {
private N data;
private Node<N> next;
}
private Node<L> head;
public List() {
head=null;
}
public void add(Node<L> node) {
Node<L> add = new Node<L>();
add.data = node.data;
add.next = null;
getFinal().next = add;
}
public Node<L> getFinal(){
Node<L> node = head;
while (node.next != null) {
node = node.next;
}
return node;
}
public int size() {
if (head == null) return 0;
int counter = 0;
for (Node<L> curr = head; curr != null; curr = curr.next)
counter++;
return counter;
}
public List<L> subList(int fromIndex, int toIndex)
throws IndexOutOfBoundsException {
List<L> n=new List<L>();
Node<L> tail= new Node<L>();
n.head=nthItem(fromIndex);
tail=n.getFinal();
tail=nthItem(toIndex);
return n;
}
public Node<L> nthItem(int index) {
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
Node<L> ptr = head;
int i = 0;
while (ptr != null) {
if (i == index) return ptr;
i++;
ptr = ptr.next;
}
throw new IndexOutOfBoundsException();
}
私有类节点{
私有数据;
私有节点下一步;
}
专用节点头;
公开名单(){
head=null;
}
公共无效添加(节点){
节点添加=新节点();
add.data=node.data;
add.next=null;
getFinal().next=add;
}
公共节点getFinal(){
节点=头部;
while(node.next!=null){
node=node.next;
}
返回节点;
}
公共整数大小(){
if(head==null)返回0;
int计数器=0;
for(节点curr=head;curr!=null;curr=curr.next)
计数器++;
返回计数器;
}
公共列表子列表(int fromIndex、int toIndex)
抛出IndexOutOfBoundsException{
列表n=新列表();
节点尾=新节点();
n、 head=第n项(从索引);
tail=n.getFinal();
尾=第n项(toIndex);
返回n;
}
公共节点项(整数索引){
如果(索引<0 | |索引>=size())
抛出新的IndexOutOfBoundsException();
节点ptr=头部;
int i=0;
while(ptr!=null){
如果(i==索引)返回ptr;
i++;
ptr=ptr.next;
}
抛出新的IndexOutOfBoundsException();
}
更新代码
private void add(Node<L> node) {
if(head==null){
head=node;
} else {
getFinal().next = node;
}
}
public List<L> subList(int fromIndex, int toIndex)
throws IndexOutOfBoundsException {
if(fromIndex<0 || fromIndex>size()-1 || toIndex<0 || toIndex>size()-1){ //size() is 1 bigger than max index so I subtract 1 from size() to equal them out
throw new IndexOutOfBoundsException();
}
List<L> n=new List<L>();
Node<L> startNode = head;
int counter=0;
while(startNode!=null){
if(counter>=fromIndex && counter<=toIndex){ //fromIndex and toIndex are inclusive so I've added the equals to them. However, it enters an infinite loop, which I do not understand why.
n.add(startNode);
}
startNode=startNode.next;
counter++;
}
return n;
}
private void add(节点){
if(head==null){
头部=节点;
}否则{
getFinal().next=节点;
}
}
公共列表子列表(int fromIndex、int toIndex)
抛出IndexOutOfBoundsException{
如果(fromIndexsize()-1 | | toIndexsize()-1){//size()比最大索引大1,那么我从size()中减去1使它们相等
抛出新的IndexOutOfBoundsException();
}
列表n=新列表();
节点startNode=头部;
int计数器=0;
while(startNode!=null){
如果(计数器>=fromIndex&&counter问题1-为什么要这样做?LinkedList可以,不需要重写
点2(或b)-您的子列表函数创建一个新列表,设置其头部,然后设置一个临时变量以包含所需的子列表尾部
在链接列表中,如果您有一个节点,则该节点表示从该点开始该列表的其余部分。要解决此问题,您需要克隆所有节点以创建子列表,并将最后一个克隆的尾部设置为null
但为什么要这样做呢
---编辑-澄清答案
public List<L> subList(int fromIndex, int toIndex) {
Node<L> currentInOriginal = nthItem(fromIndex);
int count = (toIndex - fromIndex) + 1;
List<L> newSubList = new List<L>();
newSubList.head = new Node<L>(current.data);
Node<L> lastItemInList = newSubList.head;
int soFar = 1;
currentInOriginal = currentInOriginal.next;
while(currentInOriginal!=null && soFar<count) {
lastItemInList.next = new Node<L>(currentInOriginal.data);
listItemInList = lastItemInList.next;
currentInOriginal=currentInOriginal.next;
soFar++;
}
return newSubList;
}
公共列表子列表(int-fromIndex,int-toIndex){
节点currentInOriginal=nthItem(fromIndex);
整数计数=(toIndex-fromIndex)+1;
List newpublist=新列表();
newpublist.head=新节点(当前.data);
节点lastItemInList=newPublist.head;
int-soFar=1;
currentInOriginal=currentInOriginal.next;
虽然(currentInOriginal!=null&&soFar您的方法不正确,但如果索引介于fromIndex和toIndex之间,则必须从head开始循环并返回所有值。在开始之前,您还需要验证提供的索引是否有效
这行有什么事吗
public List<L> subList(int fromIndex, int toIndex)
throws IndexOutOfBoundsException {
if(fromIndex<0 || fromIndex>size() || toIndex<fromIndex || toIndex>size()){
throw new IndexOutOfBoundsException();
}
List<L> n=new List<L>();
Node<L> startNode = head;
int counter=0;
while(startNode!=null){
if(counter>fromIndex && counter<toIndex){
n.add(startNode);
}
startNode=startNode.next;
counter++;
}
return n;
}
公共列表子列表(int-fromIndex,int-toIndex)
抛出IndexOutOfBoundsException{
如果(从IndexSize()| |到IndexSize()){
抛出新的IndexOutOfBoundsException();
}
列表n=新列表();
节点startNode=头部;
int计数器=0;
while(startNode!=null){
如果(counter>fromfindex&&counter可能您已经知道了,但是,您是否尝试过LinkedList.sublist()
?这里有一个示例:
import java.util.LinkedList;
import java.util.List;
public class GetSubListLinkedListJavaExample {
public static void main(String[] args) {
//create LinkedList object
LinkedList lList = new LinkedList();
//add elements to LinkedList
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList contains : " + lList);
/*
* To get a sublist from Java LinkedList, use
* List subList(int start, int end) method.
*
* This method returns portion of list containing element from start index
* inclusive to end index exclusive.
*/
List lst = lList.subList(1,4);
System.out.println("Sublist contains : " + lst);
/*
* Please note that sublist is backed by the original list, so any changes
* made to sublist will also be reflected back to original LinkedList
*/
//remove element from sublist
lst.remove(2);
System.out.println("Sublist now contains : " + lst);
System.out.println("Original LinkedList now contains : " + lList);
}
}
/*
Output would be
LinkedList contains : [1, 2, 3, 4, 5]
Sublist contains : [2, 3, 4]
Sublist now contains : [2, 3]
Original LinkedList now contains : [1, 2, 3, 5]
*/
希望能有帮助
克莱门西奥·莫拉莱斯·卢卡斯。使用java 8流函数,您可以从任何给定类型的列表中获取子列表
如果需要特定类型的列表,而不是列表,则可以将方法中的返回类型强制转换为该特定类型的列表
获取任何类型的给定列表(如ArrayList、LinkedList、ArrayQue)的子列表
下面的方法可用于获取任何类型的子列表
package com.techsqually.datastructure.collections.lists.linkedlist;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<Integer> giveString = new LinkedList<>();
giveString.add(1);
giveString.add(2);
giveString.add(3);
giveString.add(4);
giveString.add(3);
giveString.add(5);
System.out.println(giveString);
System.out.println(getSubList(giveString,0,3));
}
public static List getSubList(LinkedList<Integer> givenList, int startIndexInclusive, int endIndexExclusive){
return givenList.stream().collect(Collectors.toList()).subList(startIndexInclusive,endIndexExclusive);
}
}
package com.techsqually.datastructure.collections.lists.linkedlist;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.stream.collector;
公共类LinkedListTest{
公共静态void main(字符串[]args){
LinkedList giveString=新建LinkedList();
添加(1);
添加(2);
添加(3);
添加(4);
添加(3);
添加(5);
System.out.println(给定字符串);
System.out.println(getSubList(giveString,0,3));
}
公共静态列表getSubList(LinkedList givenList、int startIndexInclusive、int endIndexExclusive){
返回givenList.stream().collect(Collectors.toList()).subList(startIndexInclusive,endIndexExclusive);
}
}
输出:
[1,2,3,4,3,5]
[1,2,3]这不是一个答案OP明确表示他们正在编写自己的实现。我看不出你说什么。我清楚地写了“也许你已经知道了”。有时你在编写一个方法,并且在看了API之后发现它已经存在。而对于实际的u
package com.techsqually.datastructure.collections.lists.linkedlist;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class LinkedListTest {
public static void main(String[] args) {
LinkedList<Integer> giveString = new LinkedList<>();
giveString.add(1);
giveString.add(2);
giveString.add(3);
giveString.add(4);
giveString.add(3);
giveString.add(5);
System.out.println(giveString);
System.out.println(getSubList(giveString,0,3));
}
public static List getSubList(LinkedList<Integer> givenList, int startIndexInclusive, int endIndexExclusive){
return givenList.stream().collect(Collectors.toList()).subList(startIndexInclusive,endIndexExclusive);
}
}