java通过索引访问hashmap内部数组



我有一个形式为的Map.Entry<File, int[]>

/data/train/politics/p_0.txt, [0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
/data/train/science/s_0.txt, [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]
/data/train/atheism/a_0.txt, [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/data/train/sports/s_1.txt, [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1]

我想把这个数据放在一个二维数组的形式:

double[][] features = new double[ GLOBO_DICT.size() ][ perceptron_input.size() ];

但我不希望该文件名(标签)包含在二维数组中。

我原以为下面这样的东西会起作用,但现在我不太确定了。

 //number of features, number of x, y, z
 int size_of_globo_dict = GLOBO_DICT.size();
 //number of instances
 int NUM_INSTANCES = perceptron_input.size();
double[][] features = new double[ perceptron_input.size() ][ GLOBO_DICT.size() ];

for(int i = 0; i <= size_of_globo_dict; i++)
{
    for(int j = 0; j <= NUM_INSTANCES; j++)
    {
       features[j][i] = 
    }
}

我需要能够通过内部int数组的索引访问它,如何做到这一点?

类似于"internatl_int_array.get(instance i)"

一个简单的方法是使用for each循环迭代HashMap,如:

for(Entry<File, int[]> entry : myMap.entrySet())

对于每个条目,取值(int数组)并将其存储在矩阵中。当你转到下一个条目时,只需增加矩阵的行索引(在你的情况下是i)。

这里有一个例子,只需根据您的问题进行调整:

Map<String,int[]> myMap = new HashMap();
int []x = {1,2,3,4};
int []y = {5,6,7,8};
int []z = {9,2,3,4};
myMap.put("X",x);
myMap.put("Y",y);
myMap.put("Z", z);
int i = 0;
int [][]matrix = new int[10][10];
for(Entry<String, int[]> entry : myMap.entrySet()){
    int []a =entry.getValue();
    for(int j = 0; j < a.length; j++){
        matrix[i][j] = a[j];
    }
    i++;
}

我使用字符串作为键类型,因为我们不关心键,只关心值。迭代hashmap,获取值(int数组)并将这些值复制到矩阵中。每个条目表示矩阵中的一个新行。

最新更新