Java 如何获取2D数组中项目的索引

Java 如何获取2D数组中项目的索引,java,multidimensional-array,indexing,Java,Multidimensional Array,Indexing,在Java中,我试图从2D int数组中获取特定项的索引时遇到问题 这就是我所拥有的 private int[][] mobPoints = { {9300127,2},{9300128,2},{9300129,2},{9300130,3},{9300131,3}, {9300132,3},{9300133,3},{9300134,4},{9300135,4},{9300136,5}}; 每个数组中的第一个数字是mob标识号,第二个数字是它所值的点数。我希望它的工作方式是,当玩

在Java中,我试图从2D int数组中获取特定项的索引时遇到问题

这就是我所拥有的

private int[][] mobPoints = {
    {9300127,2},{9300128,2},{9300129,2},{9300130,3},{9300131,3},
    {9300132,3},{9300133,3},{9300134,4},{9300135,4},{9300136,5}};
每个数组中的第一个数字是mob标识号,第二个数字是它所值的点数。我希望它的工作方式是,当玩家杀死一个暴徒时,服务器会检测到,并通过一种方法发送它,该方法会根据暴徒所值的点数增加一个变量。例如:

public void addPoints(int mobid) {

}

我遇到的问题是使用给定的mobid并检索其值。我不想使用HashMaps或ArrayList,因为我似乎无法预定义它们(我必须创建一个新的ArrayList,然后在创建时添加每个值)。

这将完成工作

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
    }
}
public void addPoints(int-mobid){
//创建一个布尔值,以了解是否已找到键
布尔值=false;
//迭代矩阵数组的第一列
对于(int c=0;c
这将完成工作

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
    }
}
public void addPoints(int-mobid){
//创建一个布尔值,以了解是否已找到键
布尔值=false;
//迭代矩阵数组的第一列
对于(int c=0;c
如果希望代码能够缩放并保持性能,您可能需要尝试使用
哈希映射

    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);
            }
        }
    }
公共类移动场景{
私有HashMap mobs=新HashMap(10);
//请注意,“10”是集合的初始容量。
//我只使用它,因为我已经知道给定的容量,并避免额外的内存被保留。
公众聚众闹事现场(){
暴徒。普特(9300127,2);
暴徒。普特(9300128,2);
暴徒。普特(9300129,2);
暴徒。put(9300130,3);
暴徒。put(9300131,3);
暴徒。普特(9300132,3);
暴徒。普特(9300133,4);
暴徒。普特(9300134,4);
暴徒。普特(9300135,5);
暴徒。普特(9300136,6);
}
公共无效添加点(int mobid){
if(mobs.contains(mobid)){
mobs.put(mobs.get(mobid)+1);
}
}
}

如果希望代码能够缩放并保持性能,您可能需要尝试使用
哈希映射

    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);
            }
        }
    }
公共类移动场景{
私有HashMap mobs=新HashMap(10);
//请注意,“10”是集合的初始容量。
//我只使用它,因为我已经知道给定的容量,并避免额外的内存被保留。
公众聚众闹事现场(){
暴徒。普特(9300127,2);
暴徒。普特(9300128,2);
暴徒。普特(9300129,2);
暴徒。put(9300130,3);
暴徒。put(9300131,3);
暴徒。普特(9300132,3);
暴徒。普特(9300133,4);
暴徒。普特(9300134,4);
暴徒。普特(9300135,5);
暴徒。普特(9300136,6);
}
公共无效添加点(int mobid){
if(mobs.contains(mobid)){
mobs.put(mobs.get(mobid)+1);
}
}
}

有很多方法(例如,用一个简单的方法来定义它们)谢谢。但对于许多值来说,这有点乏味。在这里,映射绝对是正确的存储类型。随着阵列的增长,阵列查找将变得越来越慢。数组更容易写,但不值得性能更差。谢谢。我会用地图。有很多方法(比如用一个很好的短方法来定义它们)谢谢。但对于许多值来说,这有点乏味。在这里,映射绝对是正确的存储类型。随着阵列的增长,阵列查找将变得越来越慢。数组更容易写,但不值得性能更差。谢谢。我会用地图的,谢谢。我希望避免使用循环,并认为可能有一些内置功能。为此,@wonderb0lt建议您必须使用
Collections
Map
谢谢。我希望避免使用循环,并认为可能有一些内置函数。为此,@wonderb0lt建议您必须使用
Collections
Map