LinkedList的Get方法 [英] LinkedList Get method

查看:72
本文介绍了LinkedList的Get方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对单链列表有一个get方法,它们可以正常工作,但是我的讲师告诉我他希望我减少代码,因为我有太多特殊情况.问题在于,当我尝试剪切代码的某些部分时,该代码将无法正常工作.

I have a get method for my singly linked list and they work fine but my instructor told me that he wants me to cut down on the code because I have too many special cases. Problem being is that when I try to cut out some parts of my code, the code no longer works as it's supposed to.

我写的代码:

public E get(int index) {
    Node<E> f = first;

    // If index is bigger / smaller than Linked List size throw IndexOutOfBounds
    if (index > size || index < 0){
        throw new IndexOutOfBoundsException();
    }


    // Index Less than size and is not the first or last node.
    if (index < size && index > 0) {
        for (int i = 0; i < index; i++) {
            f = f.next;
        }
        return f.getValue();
    }

    // If the Linked List is empty + Index = 0 return null
    if (first == null && index == 0) {
        return null;
    }

    // If Linked List is not empty and index = 0 return first value
    if (index == 0) {
        return first.getValue();
    }

    // If index = end of list
    if (index == size) {
        return last.getValue();
    }


    // Return null if not found.
    return null;

}

所以他告诉我,我花了很多时间思考,只要索引有效或无效,只需要两种情况;我同意他的意见,因此我尝试将代码缩短为:

So he tells me that I'm putting too much thought into it and there are only two cases needed, if the index is valid or not valid; which I agree with him on, so I try to shorten my code to this:

Node<E> f = first;

// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index > size || index < 0){
    throw new IndexOutOfBoundsException();
}


// Index Less than size and is not the first or last node.
if (index <= size && index >= 0) {
    for (int i = 0; i < index; i++) {
        f = f.next;
    }
}
return f.getValue();

对于我的测试用例,我正在使用具有以下值的链表:

and for my test cases I'm using a Linked List with these values:

[弗兰克,乔治,乔希,吉姆,玛丽,苏西,约翰,吉姆,达科他州,李维斯,杰克逊,杰夫,沃尔特,马特]

[Frank, George, Josh, Jim, Marry, Susie, John, Jim, Dakota, Levi, Jackson, Jeff, Walt, Matt]

我的测试用例如下所示:

and my test cases are as follows in my test class:

System.out.println("Get Method Test ----------------------------");
System.out.println("First Index: " + ll.get(0));
System.out.println("Last Index: " + ll.get(ll.size()));
System.out.println("'Middle' Index: " + ll.get(5));
System.out.println("Empty list with index of 0: " + llempty.get(0));

在试图获取最后一个索引的第二个测试案例中抛出了 NullPointerException :

Which throws a NullPointerException at the second test case of trying to get the last index:

输出:

First Index: Frank
Exception in thread "main" java.lang.NullPointerException

我需要能够演示以下内容:

I need to be able to demonstrate the following:

测试用例:索引为零,索引在列表末尾,索引在中间"列表,索引为0的空列表,索引太大,索引太小

Test cases: index of zero, index at end of list, index in "middle" of list, empty list with index 0, index too big, index too small

所以我被困在这里的家伙们/加尔斯,任何帮助将不胜感激!

So I'm stuck here guys / gals, any help would be appreciated!

推荐答案

您的代码中有两个相同错误的实例.您必须了解索引是相对零的,这意味着大小为 n 的列表中的最后一个元素将具有索引 n-1 .第一个错误是您的新 get()方法:

There are two instances of the same mistake in your code. You have to understand that index is zero-relative, which means that the last element in a list of size n would have the index n-1. The first mistake is in your new get() method:

// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index > size || index < 0){
    throw new IndexOutOfBoundsException();
}

在这里您应该检查 index> = size ,如下所示:

Here you should check index >= size, as follows:

// If index is bigger / smaller than Linked List size throw IndexOutOfBounds
if (index >= size || index < 0){
    throw new IndexOutOfBoundsException();
}

第二个错误是您的测试代码中.在哪里写

The second mistake is in your test code. Where you write

System.out.println("Last Index: " + ll.get(ll.size()));

您应按以下方式使用 ll.size()-1 :

System.out.println("Last Index: " + ll.get(ll.size() - 1));

这两种情况都已被其他人观察到.但是,有必要同时修复它们.

Both of these things have been observed by others. It is, however, necessary to fix them both.

当我进行了这些更改并运行您的测试时,它到达了最后一行,并按预期方式抛出了 IndexOutOfBoundsException .我还测试了索引太小"和索引太大",并得到了 IndexOutOfBoundsException .

When I made those changes and ran your test it got to the last line and threw IndexOutOfBoundsException as expected. I also tested "Index Too Small" and "Index Too Big" and got IndexOutOfBoundsException.

这篇关于LinkedList的Get方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆