Home > Net >  looking for object with certain local variable in an array list
looking for object with certain local variable in an array list

Time:11-25

I have an array list "comps" of an object components each component has a specific ID. I want to search the array for a components with a certain ID use it in a method flip.

for(int i=0; i<comps.size(); i  ){
                    if(a==comps[i].sourceID){
                        comps[i].flip();
                    }
                }

I did this but it is not working as I expect it to do. I get an error saying "Array required, but ArrayList found"

CodePudding user response:

You are using ArrayList which means that you can't use brackets for indexing, what you should do is:

for(int i=0; i<comps.size(); i  ){
    if(a==comps.get(i).sourceID){
         comps.get(i).flip();
    }
}

Also I suggest you to look into streams, which makes it easier

CodePudding user response:

You are trying to access array member by []. you should try comps.get(i)

CodePudding user response:

A list does not have indexing like Arrays, use comps.get(i) for lists. For more options write down comps. and look what the IDE suggests you. There are neat things a list can do. Should look something like this:

for(int i=0; i<comps.size(); i  ) {
if( a==comps.get(i).sourceID ) {
     comps.get(i).flip(); }
}
  • Related