Thread: Help with using an iterator for an array

Results 1 to 3 of 3
  1. #1 Help with using an iterator for an array 
    Banned
    Join Date
    Nov 2014
    Age
    34
    Posts
    113
    Thanks given
    338
    Thanks received
    4
    Rep Power
    0
    So basically I have an array which holds players names, and the method checks through the names before processing whats next.
    Ex.
    Code:
    for (String comdAdvisor : nameList) {
    Declaration:
    Code:
    public static List<String> nameList;
    In loading method to initiate array list:
    Code:
    nameList = new ArrayList<String>();
    nameList.add("no entry");
    the above code this works fine, but when i go to add a person's name to the arrayList while it is looping (game running) it gives an error:
    Code:
    ConcurrentModificationException
    This is because the array can't be modified while its being looped through which causes a null.

    Code to add the players name: (name is correct)
    Code:
    nameList.add(name);
    After googling this, others had the same issue with their java projects and said to use an iterator for it as it will safely add in new elements to an array without causing it to throw an error. Although, I have never really worked with an iterator for an array before and any help would be greatly appreciated thanks.

    I understand if some stuff is unclear, please post if you need clarification. Thanks!
    Reply With Quote  
     

  2. #2  
    Success is the worst teacher

    Santa Hat's Avatar
    Join Date
    Oct 2012
    Age
    27
    Posts
    3,334
    Thanks given
    807
    Thanks received
    1,185
    Rep Power
    190
    You cannot actively modify a list whilst iterating through it, unless you are using a "CopyOnWriteArrayList" you could use this instead;

    public static CopyOnWriteArrayList<String> nameList;


    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Banned
    Join Date
    Nov 2014
    Age
    34
    Posts
    113
    Thanks given
    338
    Thanks received
    4
    Rep Power
    0
    Quote Originally Posted by Santa Hat View Post
    You cannot actively modify a list whilst iterating through it, unless you are using a "CopyOnWriteArrayList" you could use this instead;

    public static CopyOnWriteArrayList<String> nameList;
    Thanks!
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [PI] help with npc detecting and removing an item
    By xdragonlordx in forum Requests
    Replies: 1
    Last Post: 07-25-2012, 05:48 PM
  2. [PI] Help with getting my server up an running
    By Kidz K0 in forum Requests
    Replies: 1
    Last Post: 03-02-2011, 02:53 PM
  3. Help with D claw specs for Emulous
    By JohnK in forum Help
    Replies: 5
    Last Post: 03-18-2009, 12:45 AM
  4. Replies: 0
    Last Post: 02-10-2009, 05:50 AM
  5. Replies: 2
    Last Post: 10-29-2008, 09:11 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •