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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:24:13+00:00 2026-06-18T16:24:13+00:00

This is a very straightforward task, but I feel I’m overlooking something. I have

  • 0

This is a very straightforward task, but I feel I’m overlooking something. I have multiple objects that I’m trying to add to an ArrayList, and each of them has an identifying name in the form of a String. I need to be able to find (interact) with the objects in the ArrayList by calling the string name. So I tried this:

In my item class I have:
private String itemName;

public Item(String name)
{
    itemName = name;
}

So I can give it a name to be used by the user.


Then in my class that interacts with the object, I create an ArrayList:

private ArrayList<Item> items = new ArrayList<Item>();

I add an object to the arrayList first by it’s actual object name, but I need to be able to interact with it using it’s String name, so I tried this:

public void removeItem(String itemName)
{
    for (int i = 0; i < items.size(); i++)
    {
        if (items.get(i).toString() == itemName)
        {
            items.remove(i);
        }
        break;
    }

}

But it’s not removing the item. If all of this is confusing, in essence I’m trying to create an OBJECT that I can give a STRING name (like I did with the item above), then have the ability to add the OBJECT to an ArrayList, and then finally be able to remove, or get, or do something with the OBJECTS in the ArrayList by calling the STRING name. I know I need to iterate through the ArrayList, but I can’t actually get the object.

Thanks for any help.

  • 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-18T16:24:15+00:00Added an answer on June 18, 2026 at 4:24 pm

    You are dong three mistakes here:

    • You are using items.get(i).toString() which will not give you itemName for your Item. It will just give you a string representation of your Item class, returned by Object class’s toString method, if you don’t override one. However, this might work, if you have overriden a toString method, and returned the itemName from that. But, that I don’t see. And even if you have overriden that, I suggest you to have getter and setter for your itemName field, and use that to return the itemName.

    • You are comparing strings using == operator, which will not give you correct result. You should always compare the string using equals method.

    So, your if statement should look like:

     if (items.get(i).getName().equals(itemName))
    
    • 3rd problem is, you are trying to modify the List that you are iterating upon. This will not work out, and may throw ConcurrentModificationException. You should use Iterator to remove elements from the List while iterating.

    See for more details about those two problems, and how to solve them:

    • How do I compare strings in Java?
    • Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

    Further, you can consider overriding equals method in your class, and then you can directly compare your instances using equals method.


    Now, having pointed out the some logical problems with your code, it’s time to point out some design problems.

    Given your requirement, it seems like you need to use a HashMap, rather than a List of some custom type storing your attribute. You can create a map like this:

    Map<String, Integer> map = new HashMap<String, Integer>();
    

    which will contain the mapping of itemName to respective Item, and then getting the Item for a particular itemName is as simple as map.get(itemName).

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

Sidebar

Related Questions

I'm sure this issue must have a very straightforward answer but I can't seem
All, This might seems very straightforward but I have already gone through the URL
I have no idea why this is happening. I have some very straightforward code,
This should be a very straightforward problem. I have a simple .vimrc file. It
Server controls like Image.ImageUrl make this very easy, but trying to achieve the same
I have a very straightforward problem. I am using this regular expression to match
This is a very straightforward question that doesn't appear to be directly addressed at
I have this very simple example: This generates these straightforward classes: However, when I
I have this very straight forward question regarding Thread and Timer classes in Java
This might be something very straight forward and I really think it should work

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.