Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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
Java 删除ArrayList的最后一个元素_Java_Arraylist_Integer_Lastindexof - Fatal编程技术网

Java 删除ArrayList的最后一个元素

Java 删除ArrayList的最后一个元素,java,arraylist,integer,lastindexof,Java,Arraylist,Integer,Lastindexof,我是Java新手,一周多来我一直在努力解决一个练习,但我不知道我做错了什么 我需要删除ArrayList的最后几个元素,在本例中是一个整数 问题是,当我运行测试时,它仍然返回旧值 public static void removeLastOccurrence(int x, ArrayList<Integer> list) { if (list != null && !list.isEmpty()) { list.remove(list.size(

我是Java新手,一周多来我一直在努力解决一个练习,但我不知道我做错了什么

我需要删除ArrayList的最后几个元素,在本例中是一个整数

问题是,当我运行测试时,它仍然返回旧值

public static void removeLastOccurrence(int x, ArrayList<Integer> list) {
    if (list != null && !list.isEmpty()) {
       list.remove(list.size()-1);
    }
}
我还尝试使用list.removelist.lastIndexOfx

但是当我运行这个测试时,它仍然返回相同的列表

public class UTest{
    @Test
    public void testMultipleLast() {
        ArrayList<Integer> input = new ArrayList<Integer>(asList(1,1,3,5,7,1,5,9,1));
        ArrayList<Integer> result = new ArrayList<Integer>(asList(1,1,3,5,7,1,5,9));
        Solution.removeLastOccurence(1, input);
        assertEquals(result, input);
    }
}
如果有人能帮助我,告诉我我错过了什么,那将是一件非常令人沮丧的事情,因为我感觉我只是错过了拼图的一小部分。

你必须使用:

list.remove(list.size()-1);
并返回您的新列表,以便您可以使用:

public static ArrayList<Integer> removeLastOccurrence(int x, ArrayList<Integer> list) {
    if (list != null && !list.isEmpty()) {
       list.remove(list.size()-1);
    }
    return list;
}

这是因为您没有删除任何元素

list.get(list.size()-1);
不删除元素

list.get(list.size()-1);
使用

list.removelist.size-1

相反。

根据with getint index方法,您只需获取ArrayList中索引位置的元素。 这是您正在寻找的方法:

public static void removeLastOccurrence(int x, ArrayList<Integer> list) {
    if (list != null && !list.isEmpty()) {
        list.remove(list.size()-1);
    }
}

您的测试应该如下所示。在原始文章中的测试代码中,您实际上并没有调用要测试的方法

public class UTest
{
  @Test
  public void testMultipleLast() {
     ArrayList<Integer> input = new ArrayList<Integer>(asList(1,1,3,5,7,1,5,9,1));
     ArrayList<Integer> result = new ArrayList<Integer>(asList(1,1,3,5,7,1,5,9));

     // int x = ?
     ArrayList<Integer> actual = SomeClass.removeLastOccurrence(x, input)
     assertEquals(result, actual);
  }
 }

如果将列表作为参数传递给方法,它将成为局部变量。因此,您不是从输入列表中删除元素,而是仅从局部变量列表中删除元素。解决方案是从方法返回本地列表,或者使用相同的代码直接从输入列表中删除元素。原始方法中的x参数是不必要的。

在何处尝试删除元素?我没有看到任何删除?请特别参考。您的测试应该调用您正在测试的方法,对吗?!list.getlist.size-1没有删除任何内容,它返回最后一个元素在调用assertEquals之前,您必须调用removeLastOccurrence1,input;是的,没错。我猜是打字错误。删除也不起作用。我又删除了一行,因为它返回了一个错误,有人建议我删除它,你可以在我的原始帖子中看到。但据我所知,我不必更改测试代码,因为这只是为了看看代码是否正确。这根本不是真的。对对象的引用作为值传递,但对象本身的属性仍然可以更改。对输入的更改将同时反映到输入和列表中。@我同意对输入的更改将同时反映到输入和列表中,但不会反映到网站上。列表上的更改只是removeLastOccurrence方法的局部更改。这就是为什么在本例中,在调用removeLastOccurrence方法之后,输入数组仍然是相同的。它们都被更改了,因为两个变量都引用同一个对象。列表上的更改不是removeLastOccurrence方法的本地更改。请参阅,特别是传递引用数据类型参数一节。引用类型技巧!非常感谢。我应该测试并检查编辑问题的代码,而不是假设它仍然不起作用,因为它实际上按照现在的方式工作。