Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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
我不明白lastIndexOf方法在java中是如何工作的_Java_Linked List_Lastindexof - Fatal编程技术网

我不明白lastIndexOf方法在java中是如何工作的

我不明白lastIndexOf方法在java中是如何工作的,java,linked-list,lastindexof,Java,Linked List,Lastindexof,我必须编写一个名为LastIndexOf的方法,该方法接受一个整数值作为参数,并返回该值最后一次出现的列表中的索引,如果找不到该值,则返回-1。这是我的代码,但它没有返回任何东西。对我来说,它看起来总是返回-1,但我在输出中看不到它,因为它不打印方法返回的内容 这些是列出存储的值 列表->[2,5,7,24,5,9,13,2] public class LastIndexOf { public static void main(String[] args) { Sys

我必须编写一个名为LastIndexOf的方法,该方法接受一个整数值作为参数,并返回该值最后一次出现的列表中的索引,如果找不到该值,则返回-1。这是我的代码,但它没有返回任何东西。对我来说,它看起来总是返回-1,但我在输出中看不到它,因为它不打印方法返回的内容

这些是列出存储的值

列表->[2,5,7,24,5,9,13,2]

    public class LastIndexOf {

    public static void main(String[] args) {

    System.out.println("index of 5 = " + list.lastIndexOf(5)); // should to return index of 5= 4
    System.out.println("index of 100 = " + list.lastIndexOf(100)); // should return index of 100 = -1

    }

    public static int lastIndexOf (int element) {
    int index = 0;
    ListNode current = list;
    while (current != null) {
       if (current.data == element) {
           return index;
       }
       index ++;
       current = current.next;
    }
    return -1;
    }
}
这是我得到的输出:

index of 5 = 
index of 100 = 

此代码不应有2个返回语句。此外,您正在使一个节点等于整个列表;当它应该等于列表的头


这个代码是给你来纠正的吗?我之所以这样问,是因为它似乎并没有被分块开发;相反,它看起来是自上而下编写的。

此代码段返回正确的值

public class Test
{
    public static java.util.List<Integer> list = Arrays.asList(2, 5, 7, 24, 5, 9, 13, 2);

    public static void main(String[] args)
    {

        System.out.println("index of 5 = " + list.lastIndexOf(5));
        System.out.println("index of 100 = " + list.lastIndexOf(100));

        System.out.println(lastIndexOf(5));
        System.out.println(lastIndexOf(100));
    }


    public static int lastIndexOf (int element) {
    int index = 0;
    int found = -1;
    List<Integer> current = list;
    while (index < current.size()) {
       if (current.get(index) == element) {
           found = index;
       }
       index ++;
    }
    return found;
    }
}
我不知道ListNode的用途是什么,因为它实际上并不需要

我想鼓励您看看openjdk中的ArrayList实现是什么样子的:

我想使用这个:

    public int lastIndexOf (int value) {
        int index = -1;
        int size = size();
        int count = 0;

        ListNode current = front;

        for (int i = 0; i < size; i++) {
            int indexValue = current.data;
            if (value == current.data) {index = count;}
            count++;
            current = current.next;
         }
         return index;
     }

奇怪的您的输出甚至没有显示-1。。名单是什么?为什么lastIndexOfint元素是静态的?我猜list是ListNode的一个实例,您的lastIndexOf在lastIndexOf类中。是否确实list.lastIndexOf5正在调用lastIndexOfint元素?你需要检查的地方很多..是的,我展示了它上面打印的内容,它只打印:index of 5=@TemplarAnyway记住,用当前代码,你不调用你的lastIndexOf,而是调用list的lastIndexOf,它可以是一切,可以有一个方法lastIndexOf,它返回一个空字符串yeah,这是正确的@marcacierno。尝试System.out.printlnindex为5=+lastIndexOf5;我认为他使用了列表的自定义实现,这就是为什么它对您有效。您可能是对的。无论如何,这个想法保持不变,实际上,它应该从后面检查,但我希望尽可能接近OP实现。