0

I'm having problems trying to get the index of a specific item from a 2D int array in Java.

So here's what I have ...

private int[][] mobPoints = {
    {9300127,2},{9300128,2},{9300129,2},{9300130,3},{9300131,3},
    {9300132,3},{9300133,3},{9300134,4},{9300135,4},{9300136,5}};

The first number in each of the arrays is the mob identification number, and the second number is the number of points it's worth. How I want it to work is when a player kills a mob, the server detects that and sends it through a method that increments a variable with the amount of points the mob is worth. Example:

public void addPoints(int mobid) {

}

What I'm having trouble doing is using the given mobid and retrieving the value it is worth. I don't want to use HashMaps or ArrayLists because I can't seem to predefine them (I have to create a new ArrayList and then add each and every value upon creation).

Jacob Macallan
  • 959
  • 2
  • 8
  • 29
  • There are ways to ["predefine" a map](http://stackoverflow.com/q/7345241/660848) (i.e. having a nice short way to define them) – wonderb0lt Jul 17 '15 at 11:33
  • Thanks. It's a bit tedious for many values though. – Jacob Macallan Jul 17 '15 at 11:39
  • A map is definitely the right storage type here. Your array lookups will become slower and slower as the array grows. An array is a bit easier to write down, but that's not worth worse performance. – wonderb0lt Jul 17 '15 at 11:43

2 Answers2

3

You might want to try using a HashMap<Integer, Integer> instead if you want the code to scale and stay performant.

    public class MobScene {
        private HashMap<Integer, Integer> mobs = new HashMap<Integer, Integer>(10);
        // Note that '10' is the initial capacity of the Collection.
        // I only use it as I already know the given capacity and avoid extra memory being reserved.

        public MobScene() {
            mobs.put(9300127,2);
            mobs.put(9300128,2);
            mobs.put(9300129,2);
            mobs.put(9300130,3);
            mobs.put(9300131,3);
            mobs.put(9300132,3);
            mobs.put(9300133,4);
            mobs.put(9300134,4);
            mobs.put(9300135,5);
            mobs.put(9300136,6);
        }

        public void addPoints(int mobid) {
            if(mobs.contains(mobid)) {
                mobs.put(mobs.get(mobid) + 1);
            }
        }
    }
Genkus
  • 151
  • 6
1

This will do the work....

public void addPoints(int mobid) {
    // create a boolean to know if key has been found
    boolean found = false;

    // iterate over first column of your matrix array
    for (int c = 0; c < mobPoints.length; c++) {
        // if the key still not found and is equal first column value 
        if (!found && mobPoints[c][0] == mobid) {
            // add points or do your stuff
            System.err.println("Value = " + mobPoints[c][1]);
            // mark as found
            found = true;
        }
    }
    if (!found) {
        // not found error
    }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109