• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Arraylist looking for a match

I have a arraylist named rds.  The arraylist is setup like this

tcRevison, dataset, mformUid

I am trying to do a compare on mformUid.

Will this work?

for(int i = 0; i < changedObjs.length; i++){
   String changedItem = changedObjs[i].getUid();
   if(rds.get(i).mformUid.contains(changedItem)) {
      fireTableDataChanged();
    }
  }

Open in new window


I am wanting to see if changedItem matches any mformUid in the rds arraylist
0
jkteater
Asked:
jkteater
  • 2
3 Solutions
 
CEHJCommented:
Difficult to know whether it will work. Looks logical
0
 
ariohCommented:
maybe it would be better to add break; after fireTableDataChanged();   :)
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
No, this wont work. You will need an extra (nested) loop to iterate through the rds arraylist. Like so...

for(int i = 0; i < changedObjs.length; i++){
   String changedItem = changedObjs[i].getUid();
   for(int j = 0; j < rds.length; j++){
    if(rds.get(j).mformUid.contains(changedItem)) {
      fireTableDataChanged();
      break;
    }
   }
  }

Open in new window


Of course, there may be opportunities here to optimise this code, but you would need to tell us more info like is this check likely to happen a lot more times than any code the adds items to the list, etc.
0
 
CEHJCommented:
maybe it would be better to add break; after fireTableDataChanged();   :)

That's actually a good point. Unless (and you should try to do this imo) you try to provide finer-grained TableModelEvent instances to listeners. Then you wouldn't break if you can make all changes count more.
But if you're going to just issue a 'blanket' event, then you might as well break immediately after the first change is registered as that would be more efficient.

mccarl might have a good point too - if there's no identity between the length of array 'changedObjs' and 'rds'. I have no way of knowing that
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now