remove last element from arraylist java
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.remove(list.size()-1); //removes 3
remove last element from arraylist java
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.remove(list.size()-1); //removes 3
How to remove last element from arraylist in java
// How to remove last element from arraylist in java
import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveLastElement
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(56);
al.add(28);
al.add(39);
al.add(59);
al.add(82);
// using size() method to find index of last element
int index = al.size() - 1;
// deleting last element by passing index
al.remove(index);
System.out.println("After remove last element from arraylist: " + al);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us