Hi everyone,

I have a single string - let's call it string A.
I have an ArrayList of objects.
Each object contains three strings.

I want to iterate through the ArrayList of objects and check if any of the strings contained within the objects are the same as string A. Furthermore, I only want to compare the third string in each object with string A.

Basically, I am having problems with the syntax for a string contained in an object, and any help would be great.

Here's the part of code I'm having trouble with:

while( iterator.hasNext() )
{
    if( iterator.string3.equals( stringA ) )
    {
                
    }
}

If anyone could help me with this approach, or give me any ideas for a different approach, that would be a big help.

Thanks!

Hi everyone,

I have a single string - let's call it string A.
I have an ArrayList of objects.
Each object contains three strings.

I want to iterate through the ArrayList of objects and check if any of the strings contained within the objects are the same as string A. Furthermore, I only want to compare the third string in each object with string A.

Basically, I am having problems with the syntax for a string contained in an object, and any help would be great.

Here's the part of code I'm having trouble with:

while( iterator.hasNext() )
{
    if( iterator.string3.equals( stringA ) )
    {
                
    }
}

If anyone could help me with this approach, or give me any ideas for a different approach, that would be a big help.

Thanks!

Should I just break this down into 2 steps?

I.e. have an outer loop going through the objects and an inner loop going through the strings?

that might be the best, you'll have to do that anyway, since you say there are three String objects in every element in the ArrayList

It looks like you're having trouble with your iterator. To access the next object you need

while( iterator.hasNext() )
{
    YourObjectType nextObj = (YourObjectType)iterator.next();
    if( nextObj.string3.equals( stringA ) )
    {
                
    }
}

Where "YourObjectType" is of course whatever object you're using that contained the strings.

Thanks for your help guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.