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 9198027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:11:42+00:00 2026-06-17T22:11:42+00:00

I have 2 ArrayList, one containing String s, the other Integer s. The list2

  • 0

I have 2 ArrayList, one containing Strings, the other Integers. The list2 contains indices of elements of list1.

Now I need to remove all the elements from list1 whose index is in list2. Any ideas?

ArrayList<String> list1 = new ArrayList<String>(); 
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
list1.add("e");
list1.add("f");

ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(1);
list2.add(4);
list2.add(2);

The problem here is, you cannot remove from original list1 as the index will keep changing. I tried creating a temp HashMap to store the array index and String relation.

I iterate over list2 and map. When I found a matching key=index, I skipped that. Else I put String element in new list.

Any better suggestions?

  • 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-17T22:11:43+00:00Added an answer on June 17, 2026 at 10:11 pm

    This solution assumes that null is not a valid value in list1.

    You could iterate through list2, and for each index you get to, set the corresponding value in list1 to null. Then remove all the nulls from list1 at the end. This will work even if there are duplicate elements in list2, which jlordo’s solution would have difficulty with.

    for(Integer index : list2){
        list1.set(index,null);
    }
    list1.removeAll(Collections.singleton(null));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have an ArrayList containing Objects that have a date value. Now I want
I have one ArrayList of 10 String s. How do I update the index
Situation: I have an ArrayList<String> containing a bunch of links to images (http:/www.foo.com/bar/image1.jpg, http:/www.foo.com/bar/image2.png,...
I have a ArrayList, with elements something like: [string,has,was,hctam,gnirts,saw,match,sah] I would like to delete
I am getting this strange output in HashMap . I have two ArrayList<String> one
Let's assume we have a java.util.LinkedList containing a few hundreds of elements. One possibility
I have one question. In Java I have an ArrayList containing an ArrayList with
I have one ArrayList in Session, lets say for example [305,306,380] . On submit,
I have an ArrayList of objects in one activity and I need this arrayList

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.