Solved ArrayList.add() wont work?

Discussion in 'Plugin Development' started by ItsComits, Dec 14, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    ItsComits

    Hello, When trying to loop through a hashmap for a specific key. I want to be able to add the values for that specific key to an array. I did some debug and the arrayList gets one location added on. Even though there is 2.
    Here is the code:
    Code:
        public Location[] getLocationsForID(int ID) {
            List<Location> list = new ArrayList<Location>();
            Location[] array = new Location[list.size()];
            for (Entry<Integer, Location> map : this.locations.entrySet()) {
                if (map.getKey().equals(ID)) {
                    list.add(map.getValue());
                }
            }
            return list.toArray(array);
        }
    Any help regarding this is greatly appreciated. :D
     
  2. Offline

    timtower Administrator Administrator Moderator

    @ItsComits Map can only have 1 instance of a key though.
    So if you input 1 twice then you still have 1 entry.
     
  3. Offline

    ItsComits

    So how would I go about adding them all at once?
     
  4. Offline

    timtower Administrator Administrator Moderator

    Your map is the issue, not the array or the list.
    If you want to store more values for a key then the value should be a list.
     
Thread Status:
Not open for further replies.

Share This Page