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

  • Home
  • SEARCH
  • 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 1016281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:31:34+00:00 2026-05-16T10:31:34+00:00

I have an ArrayList, which includes a number of items I want to remove.

  • 0

I have an ArrayList, which includes a number of items I want to remove. I have the ids of the items to remove stored in another list. Figured the following code should work trivially, but for some reason, the remove() calls are returning a false value:

  ArrayList<Integer> toRemove = new ArrayList<Integer>();
  ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();

  /* Code that adds a bunch of items to al, and a few integers to toRemove */

  System.out.println("Size before removing: " + al.size());
  for (int i = toRemove.size() - 1; i >= 0; i--) {
    System.out.println("Removing id: " + toRemove.get(i) + ": ");
    System.out.println(al.get(toRemove.get(i)));
    System.out.println(al.remove(toRemove.get(i)));
  }
  System.out.println("Size after removing: " + al.size());

I’d get it if the get() call also returned a false value, but it does actually return the object in question. What am I missing here?

The output of the code above:

Size before removing: 3
Removing id: 2: 
javax.swing.JCheckBox[...]
false
Size after removing: 3
  • 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-05-16T10:31:35+00:00Added an answer on May 16, 2026 at 10:31 am

    My guess is you are having a problem with the fact that remove() is overloaded with both int and Object, while get() only takes an int. Try remove(toRemove.get(i).intValue()).

    remove(Object) from AbstractCollection will search through the list and remove the given object, which won’t be there because you are sending it an Integer and the list only has JCheckBoxs. You are trying to call remove(int), but because you are giving it an Integer, the Object overload is called instead. By converting the Integer to an int, you avoid this problem

    Also, can you always be sure the Id in toRemove always equals the index? If toRemove is not in greatest to least order, it won’t be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which is adding somestrings to an arraylist. It will
I have Grid which will render a calendar, and I'm provided with an ArrayList<CalendarEventEntity>
I have the following class which I'm fighting over how to implement. The question
in c# if i have an arraylist populated like (ID, ITEMQUANTITY), and i would
I have an object called FormObject that contains two ArrayLists - oldBooks and newBooks
I have this situation: web application with cca 200 concurent requests (Threads) are in
I have two activities, activity 1 and activity 2 for example. Activity 1 will
I'm making a method to fetch a list of filenames from a server but
I am working on an application where i am saving the state of an

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.