I have a Map.Entry<File, int[]>
of the form:
我有一个Map.Entry
/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]
I want to put this data to a 2D array of the form:
我想将此数据放入表单的2D数组中:
double[][] features = new double[ GLOBO_DICT.size() ][ perceptron_input.size() ];
But I don't want that file name (label) to be included in the 2D array.
但我不希望文件名(标签)包含在2D数组中。
I was thinking something like the following would do the trick but now I'm not so certain.
我在想像下面这样的东西可以做到这一点,但现在我不太确定。
//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] =
}
}
I need to be able to access the internal int array by it's index, how to do that?
我需要能够通过它的索引访问内部int数组,怎么做?
something like 'internatl_int_array.get( instance i)'
比如'internatl_int_array.get(instance i)'
1 个解决方案
#1
1
A simple way would be to iterate over the HashMap using a for each loop like:
一个简单的方法是使用for循环遍历HashMap,如:
for(Entry<File, int[]> entry : myMap.entrySet())
For every entry, take the value (the int array) and store it in the matrix. When you go to the next entry, just increment the line index (i in your case) for the matrix.
对于每个条目,取值(int数组)并将其存储在矩阵中。当您转到下一个条目时,只需为矩阵增加行索引(在您的情况下为i)。
Here is an example, just adapt it to your problem:
这是一个例子,只是适应你的问题:
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++;
}
I used String as Key type, since we don't care about the key, only about the value. Iterate the hashmap, get the value (the int array) and copy those values into the matrix. Every entry represents a new line in the matrix.
我使用String作为Key类型,因为我们不关心键,只关心值。迭代hashmap,获取值(int数组)并将这些值复制到矩阵中。每个条目代表矩阵中的新行。
#1
1
A simple way would be to iterate over the HashMap using a for each loop like:
一个简单的方法是使用for循环遍历HashMap,如:
for(Entry<File, int[]> entry : myMap.entrySet())
For every entry, take the value (the int array) and store it in the matrix. When you go to the next entry, just increment the line index (i in your case) for the matrix.
对于每个条目,取值(int数组)并将其存储在矩阵中。当您转到下一个条目时,只需为矩阵增加行索引(在您的情况下为i)。
Here is an example, just adapt it to your problem:
这是一个例子,只是适应你的问题:
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++;
}
I used String as Key type, since we don't care about the key, only about the value. Iterate the hashmap, get the value (the int array) and copy those values into the matrix. Every entry represents a new line in the matrix.
我使用String作为Key类型,因为我们不关心键,只关心值。迭代hashmap,获取值(int数组)并将这些值复制到矩阵中。每个条目代表矩阵中的新行。