0

I have a Map.Entry<File, int[]> of the form:

/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:

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.

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?

something like 'internatl_int_array.get( instance i)'

9
  • Are you perhaps just looking for Map.values()? You should bear in mind that the order of entries in a map may well be unstable. Commented Feb 18, 2015 at 9:11
  • what do you mean by that? I think that's not what i want. i want, for instance something like 'internatl_int_array.get( instance i)' Commented Feb 18, 2015 at 9:13
  • But what would you want that to do? It's really unclear what you're trying to achieve, I'm afraid. It would help if you could show a short but complete program instead of just snippets with unconventional names of variables whose types we can't see... Commented Feb 18, 2015 at 9:19
  • @flavius_valens And what's wrong with using [i]? If you want an interface for array, just wrap an array in your object and write getters/setters/whatever interface you want for an array. For whatever purpose it might serve. Commented Feb 18, 2015 at 9:20
  • How come features is defined as new double[ GLOBO_DICT.size() ][ perceptron_input.size() ] in the second code block but as new double[ perceptron_input.size() ][ GLOBO_DICT.size() ] in the third code block? Commented Feb 18, 2015 at 9:21

1 Answer 1

1

A simple way would be to iterate over the HashMap using a for each loop like:

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

that sounds reasonable but my brain is sort of fried right now. could you please show me what your talking about?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.