Check Array Contents with ArrayList

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

  • edited November 2013

    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.

  • edited April 2014

    Can someone recommend something?

    Well, 'twas much easier for me to come up w/ something else! #:-S

    /** 
     * No Repeat ID Input (v1.10)
     * by GoToLoop (2013/Nov)
     * 
     * forum.processing.org/two/discussion/869
     * /check-array-contents-with-arraylist
     */
    
    import static javax.swing.JOptionPane.*;
    
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    } 
    );
    
    void draw() {
      println(ids);
    
      final String id = showInputDialog("Please enter new ID");
    
      if (id == null)   exit();
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
    
      else if (ids.hasValue(id))
        showMessageDialog(null, "ID \"" + id + "\" exists already!!!", 
        "Alert", ERROR_MESSAGE);
    
      else {
        showMessageDialog(null, "ID \"" + id + "\" successfully added!!!", 
        "Info", INFORMATION_MESSAGE);
    
        ids.append(id);
      }
    }
    
  • edited November 2013

    I think you need to add some intial value in to the ArrayList

            String[] usersIDs = {
              "001", "002", "003" , "004"
            };
    
            ArrayList<String> identifiedUsers = new ArrayList<String>();
            void setup() {
              identifiedUsers.add("Aabh"); // addsome intial value 
            }
    
            void draw() {
              for (int i=0;i<identifiedUsers.size();i++) {
                String str = (String) identifiedUsers.get(i);
                println(str);
              }
            } 
    
            void mousePressed() {
              for (int y=0; y<usersIDs.length; y++) {
                for (int i=0; i<identifiedUsers.size(); i++) {
                  if (identifiedUsers.get(i)!=(usersIDs[y])) {
                    identifiedUsers.add(usersIDs[y]);
                  } 
                  else {
                  }
                }
              }
            }
    

    Or try this both give same result

              String[] usersIDs = {
                "001", "002", "003", "004"
              };
    
              ArrayList<String> identifiedUsers = new ArrayList<String>();
              void setup() {
                //identifiedUsers.add("Aabh"); // addsome intial value
              }
    
              void draw() {
                if (identifiedUsers.size()==0) identifiedUsers.add("X");
                for (int i=0;i<identifiedUsers.size();i++) {
                  String str = (String) identifiedUsers.get(i);
                  println(str);
                }
              } 
    
              void mousePressed() {
                for (int y=0; y<usersIDs.length; y++) {
                  for (int i=0; i<identifiedUsers.size(); i++) {
                    if (identifiedUsers.get(i)!=(usersIDs[y])) {
                      identifiedUsers.add(usersIDs[y]);
                    } 
                    else {
                    }
                  }
                }
              }
    
  • Answer ✓

    Try this code

    String[] usersIDs = {
      "001", "002", "003", "004", "005"
    };
    
    ArrayList<String> identifiedUsers = new ArrayList<String>();
    
    void setup() {
      addUserID(identifiedUsers, usersIDs[3]);
      addUserID(identifiedUsers, usersIDs[2]);
      addUserID(identifiedUsers, usersIDs[3]);
      addUserID(identifiedUsers, usersIDs[4]);
      addUserID(identifiedUsers, usersIDs[0]);
    }
    
    boolean addUserID(ArrayList<String> users, String userID) {
      if (!users.contains(userID)) {
        users.add(userID);
        println(userID + " has been added to list");
        return true;
      }
      println(userID + " cannot be added to list because already there");
      return false;
    }
    

    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

  • edited November 2013

    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:

    boolean removeUserID(ArrayList<String> users, String userID) {
      if (users.contains(userID)) {
        users.remove(userID);
        println(userID + " has been remove from the list");
        return true;
      }
      println(userID + " cannot be found in the list");
      return false;
    }
    
  • edited November 2015
    String[] usersIDs = {
      "001", "002", "003", "004", "005"
    };
    
    ArrayList<String> identifiedUsers = new ArrayList<String>();
    
    void setup() 
    {
      background(0);
      addUserID(identifiedUsers, usersIDs[0]);
      addUserID(identifiedUsers, usersIDs[1]);
      addUserID(identifiedUsers, usersIDs[2]);
      addUserID(identifiedUsers, usersIDs[3]);
      addUserID(identifiedUsers, usersIDs[4]);
    }
    
    boolean addUserID(ArrayList<String> users, String userID) {
      if (!users.contains(userID)) {
        users.add(userID);
        println(userID + " has been added to list");
        return true;
      }
      println(userID + " cannot be added to list because already there");
      return false;
    }
    

    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

Sign In or Register to comment.