Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:17:47+00:00 2026-06-17T00:17:47+00:00

I have ArrayList, from which I want to remove an element which has particular

  • 0

I have ArrayList, from which I want to remove an element which has particular value…

for eg.

ArrayList<String> a=new ArrayList<String>();
a.add("abcd");
a.add("acbd");
a.add("dbca");

I know we can iterate over arraylist, and .remove() method to remove element but I dont know how to do it while iterating.
How can I remove element which has value “acbd”, that is second element?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:17:49+00:00Added an answer on June 17, 2026 at 12:17 am

    In your case, there’s no need to iterate through the list, because you know which object to delete. You have several options. First you can remove the object by index (so if you know, that the object is the second list element):

     a.remove(1);       // indexes are zero-based
    

    Or, you can remove the first occurence of your string:

     a.remove("acbd");  // removes the first String object that is equal to the
                        // String represented by this literal
    

    Or, remove all strings with a certain value:

     while(a.remove("acbd")) {}
    

    It’s a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can’t remove them by using remove with an object that is equal to the one you want to delete.

    In those case, I usually use a second list to collect all instances that I want to delete and remove them in a second pass:

     List<MyBean> deleteCandidates = new ArrayList<>();
     List<MyBean> myBeans = getThemFromSomewhere();
    
     // Pass 1 - collect delete candidates
     for (MyBean myBean : myBeans) {
        if (shallBeDeleted(myBean)) {
           deleteCandidates.add(myBean);
        }
     }
    
     // Pass 2 - delete
     for (MyBean deleteCandidate : deleteCandidates) {
        myBeans.remove(deleteCandidate);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add both String and Integer type of value in ArrayList. For
i have an arraylist and i want to add and remove many items at
I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have two arraylist ArrayList<String> a1 ArrayList<String> a2 I want to check if all
I have an ArrayList which stores Area names. I want to check this list
I want to split data from file which have the following format: {[(1;1;2),(5;1;2),(5;1;1)],[(1;3;2),(5;3;2),(5;1;2)]} And
I currently have an arraylist in which I add blossom objects ( which fall
I have an arraylist which contain objects of my own class. I want to
I have 2 elements tags which i want to add to 2 edittexts of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.