How to remove an element from ArrayList in Java

How to remove an element from ArrayList in Java - Hello friend inabnomaniiyaha, In the article that you read this time with the title How to remove an element from ArrayList in Java, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel Code-java-example,what we write you can understand. Alright, happy reading.

Judul : How to remove an element from ArrayList in Java
link : How to remove an element from ArrayList in Java

Baca juga


How to remove an element from ArrayList in Java

How to remove an element from ArrayList in Java

1. By using remove() methods 

remove(int index)

import java.util.List;

import java.util.ArrayList;

public class GFG

{

    public static void main(String[] args)

    {

        List<Integer> al = new ArrayList<>();

        al.add(10);

        al.add(20);

        al.add(30);

        al.add(1);

        al.add(2);

  

        // This makes a call to remove(int) and 

        // removes element 20.

        al.remove(1);

          

        // Now element 30 is moved one position back

        // So element 30 is removed this time

        al.remove(1);

  

        System.out.println("Modified ArrayList : " + al);

    }

}

Output :

Modified ArrayList : [10, 1, 2]

remove(Obejct obj) 

import java.util.List;

import java.util.ArrayList;

public class GFG

{

    public static void main(String[] args)

    {

        List<Integer> al = new ArrayList<>();

        al.add(10);

        al.add(20);

        al.add(30);

        al.add(1);

        al.add(2);

  

        // This makes a call to remove(Object) and 

        // removes element 1

        al.remove(new Integer(1));

          

        // This makes a call to remove(Object) and 

        // removes element 2

        al.remove(new Integer(2));

  

        System.out.println("Modified ArrayList : " + al);

    }

}

Output :

Modified ArrayList : [10, 20, 30]

2. Using Iterator.remove()

import java.util.List;

import java.util.ArrayList;

import java.util.Iterator;

public class GFG

{

    public static void main(String[] args)

    {

        List<Integer> al = new ArrayList<>();

        al.add(10);

        al.add(20);

        al.add(30);

        al.add(1);

        al.add(2);

  

        // Remove elements smaller than 10 using

        // Iterator.remove()

        Iterator itr = al.iterator();

        while (itr.hasNext())

        {

            int x = (Integer)itr.next();

            if (x < 10)

                itr.remove();

        }

        System.out.println("Modified ArrayList : "

                                           + al);

    }

}

Output :

Modified ArrayList : [10, 20, 30]



That's the articleHow to remove an element from ArrayList in Java

That's it for the article How to remove an element from ArrayList in Java this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleHow to remove an element from ArrayList in Java with link addresshttps://inabnonapudyawanabing.blogspot.com/2021/05/how-to-remove-element-from-arraylist-in.html

0 Response to "How to remove an element from ArrayList in Java"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...