Results 1 to 3 of 3
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Remove string from arraylist when not updated (more info inside)

    (Not related to the networking, it's about array list)
    Hello, basically I've got few array lists that holds strings like this "name", "x", "y" and it's always updating shortly
    It's a multiplayer client that gets usernames and their coordinates, if new user connected then server sends coordinates of the new user. If the new user not exist in the arraylist, then add him.

    ArrayList<String> names = ArrayList<String>();
    ArrayList<String> userX = ArrayList<String>();
    ArrayList<String> userY = ArrayList<String>();

    But the questions is, how do I remove the name and his coordinates from the array list if he is not getting any updates?
    Basically it works like this:
    Imagine that this string came from the server:
    Java Code:
    String info = "john 10 11 1;mark 5 50 1;vosiliy 4 3 1";
    I split this string with ";"
    Java Code:
    String[] extractedInfo = info.split(";");
    then I just do a for loop:
    Java Code:
    for(int i = 0; i<extractedInfo.length();i++){
             boolean nameExist = false; //gonna check if name is already in the arraylist
             String[] currentInfo = extractedInfo[i].split(" "); //to separate the arguments in the string..
                      for(int l =0; l<names.size();l++;){
                               if(b[0].equals(name.get(l)){
                                         userX.set(l, currentInfo[1]);//0 = name, 1 = X, 2=Y
                                         userY.set(l, currentInfo[2]);
                                         nameExist = true;
                               }
                      }
                      if(!nameExist){
                             names.add(currentInfo[0]);
                             userX.add(currentInfo[1]);
                             userY.add(currentInfo[2]);
                      }
    }
    The string is always changes each name got different userX and userY, some times new user connects and sometimes disconnects
    When connecting I've made him to be in the arraylist, but when disconnecting, the server stops sending his info, so I want:
    if user is not in the string info then remove him from the array list and his coordinates.
    How I've been breaking my head and don't know how to do that xD Help please?
    WARNING I am Russian so it's possible that I wont understand you correctly...

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: Remove string from arraylist when not updated (more info inside)

    Study the API of the ArrayList and you will see that this class has a method contains() to check if an element is already in the list or a method remove() to remove an element for the list. Have fun ...

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Remove string from arraylist when not updated (more info inside)

    Quote Originally Posted by j2me64 View Post
    Study the API of the ArrayList and you will see that this class has a method contains() to check if an element is already in the list or a method remove() to remove an element for the list. Have fun ...
    Ehh thanks but I knew how to remove from the array list, I just needed a way that checks if server didn't sent the data for the user that still in the game, so I can remove him...
    just make a for loop and info.indexOf(names.get(i))<0 { names.remove(i);

    But thanks for trying to help
    But basically that was actually very
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Why can not I remove an object from an arrayList?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 11-21-2011, 01:37 PM
  2. ArrayList and remove()
    By Pragz in forum New To Java
    Replies: 1
    Last Post: 04-21-2011, 01:42 AM
  3. Replies: 8
    Last Post: 10-08-2010, 07:24 AM
  4. need help with the remove method on arrayList
    By ShinTec in forum New To Java
    Replies: 5
    Last Post: 02-16-2010, 09:38 AM
  5. Replies: 1
    Last Post: 12-02-2009, 03:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •