Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P03 method subList #19

Open
tianhaolin1991 opened this issue Jan 23, 2019 · 0 comments
Open

P03 method subList #19

tianhaolin1991 opened this issue Jan 23, 2019 · 0 comments

Comments

@tianhaolin1991
Copy link

tianhaolin1991 commented Jan 23, 2019

I think you'd better use my codes below:

 public static <E> E findKthRecursive(LinkedList<E> list, Integer k){
        if(list.size()==0 || list==null){
            throw new NoSuchElementException("No elements in this list");
        }
        if(k >= list.size()){
            throw new IndexOutOfBoundsException("Index k is out of bounds");
        }
        if(k==0) return list.pollFirst();
        list.pollFirst();
        return findKthRecursive(list,k - 1);
    }

because the method sublist will create a SublistObject which has a field references the original list,if the original is very large,you will have a Memory leak,because the original list will never GC by JVM until your sublist without use.

PS:my English is very bad,so if anywhere offend/confuse you , or I say somthing wrong ,thanks for your feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant