We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi!
I have a string array of user ids, and I would like to add a new id to my arraylist, only when this value is not in the arraylist.
I do the following, but I don't get any results. Can someone recommend something?
String[] usersIDs = {
"001", "002", "003"
};
ArrayList<String> identifiedUsers = new ArrayList<String>();
void keyReleased() {
if (key == 'a') {
for (int y=0; y<usersIDs.length; y++) {
for (int i=0; i<identifiedUsers.size(); i++) {
if (identifiedUsers.get(i).equals(usersIDs[y])) {
println("a");
}
else {
println("b");
identifiedUsers.add(usersIDs[0]);
}
}
}
}
}
Answers
I am sorry, but it is unclear... Is it the only code you use? So
identifiedUsers
is empty at the start?Why do you add always the first
usersIds
entry?Do you plan to add several ids at once or only one on key released? (A
break
can help in the latter case.)Maybe a Set can be useful. Since I don't know why you need to do that, it is hard to give precise advices.
Well, 'twas much easier for me to come up w/ something else! #:-S
I think you need to add some intial value in to the ArrayList
Or try this both give same result
Try this code
Gives the output
004 has been added to list
003 has been added to list
004 cannot be added to list because already there
005 has been added to list
001 has been added to list
Thank you all so much with the suggestions. I have tried them all - and they work just fine - however, quark's solution seems more appropriate in this occasion. And I also added a remove function to remove the arraylist contents:
how are you entering this values? if when the draw window opens up theres no actual input bar? i can see this working very good using Arduinos IDE Terminal and using the cout and cin calls