简体   繁体   中英

Removing the last element of an ArrayList

I'm new to Java and I'm stuck with an exercise I've been trying to solve for over a week now and I don't know what I'm doing wrong.

I need to delete the last elements of an ArrayList, an integer in this case.

The problem is that when I run the test, it still returns the old values.

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

I also tried using list.remove(list.lastIndexOf(x));

But it still returns the same list when I run this test.

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);
    }
}

Would be nice if someone could help and tell me what I'm missing as it's getting quite frustrating as I've the feeling that I'm just missing a small piece of the puzzle.

Your test should be like below. In the test code in the original post, you are not actually invoking the method that you are trying to test.

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);
  }
 }

and the removeLastOccurrence() method can do the following

if(list != null && !list.isEmpty()){
    list.remove(list.size() - 1);
}

You have to use :

list.remove(list.size()-1);

And return your new list so you can use :

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

It's because you are not removing any elements.

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

does not remove elements.

use

list.remove(list.size()-1)

instead.

According to Java ArrayList API with get(int index) method you just obtain the element in index position in your ArrayList. This is the method you are looking for:

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

If you pass your list as argument to the method it becomes a local variable. Therefore you are not removing the element from your input list but only from the local variable list . The solution is to return that local list from your method or to remove the element directly from your "input" list using the same code. The x parameter in your original method is unnecessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM