views:

266

answers:

4

I have the following code which adds some arrays to a hashmap but then I want access those arrays to do some work on them later. I've gotten this far but can't figure the rest out to make it work....

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

    public static void main(String[] args) {

        String[][] layer1 = {
            {"to1", "TYPE1", "start"}, 
            {"to2", "TYPE1", "start"}
        };

        String[][] layer2 = {
            {"to3", "TYPE2" ,"item1"},
            {"to3", "TYPE2" ,"item2"}
        };

        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("layer1", layer1);
        hashMap.put("layer2", layer2);

        Iterator<Entry<String, Object>> iterator = hashMap.entrySet().iterator();
        while(iterator.hasNext()){
            hashMap.values().toArray();
            for (???) {
                // lets print array here for example
            }
        }        

    }
+4  A: 

Smells like homework, but a few suggestions -

  • there's no reason for your Hashmap to be of the form <String,Object> - make it <String, String[][]> , as that's what you're storing.

You're iterating twice. You either - iterate through the map, either the keys, values or entries. Each item is an iterator return value, e.g.

for (String[][] s:map.values()){
 ...
}
  • hashmap.values.toArray gives you all of the contents, which is the same thing your iterator is doing.

  • if you're only iterating through the contents, then you're not really using a map, as you're never making use of the fact that your values are available by key.

Steve B.
not homework... just mixing too many languages to learn atm :) Thanks!
EddyR
A: 

Your while loop should look like -

        while(iterator.hasNext()){
            String[][] arr = (String[][])iterator.next().getValue();

            for (String[] strings : arr) {
                for (String string : strings) {
                    System.out.println(string);
                }
            }
        }
Gopi
A: 

Your code is bad formed here:

while(iterator.hasNext()){
    hashMap.values().toArray();
    for (???) {
        // lets print array here for example
    }
}      

You try to iterate using the Iterator "iterator" but next you call to

hashMap.values().toArray();

To get the next item of the loop you need to use iterator.next(); to fetch it. Also is good to change the "Object" by String[][] or to List<String[]> or List<List<String>>.

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Map.Entry;

public static void main(String[] args) {

    String[][] layer1 = {
        {"to1", "TYPE1", "start"}, 
        {"to2", "TYPE1", "start"}
    };

    Map<String,String[][]> map= new HashMap<String,String[][]>();
    map.put("layer1", layer1);

    Iterator<Entry<String, String[][]>> iterator = map.entrySet().iterator();
    while(iterator.hasNext()){
        Entry<String, String[][]> entry = iterator.next();
        System.out.println("Key:" + entry.getKey());

        String[][] value = entry.getValue();
        for(int x=0;x<value.length;x++){
         for(int y=0;y<value[x].length;y++){
          System.out.println("String[" + x + "][" + y + "]:" + value[x][y]);        
         }
        }
    }        
}

Also you can use "for each" loop to simplify the code insted using the "while":

for (Entry<String, String[][]> entry : map.entrySet()){
    System.out.println("Key:" + entry.getKey());
    String[][] value = entry.getValue();
    for(int x=0;x<value.length;x++){
        for(int y=0;y<value[x].length;y++){
            System.out.println("String[" + x + "][" + y + "]:" + value[x][y]);      
        }
    }
}

Or if you only need the values:

for (Entry<String, String[][]> entry : map.values()){
    String[][] value = entry.getValue();
    for(int x=0;x<value.length;x++){
        for(int y=0;y<value[x].length;y++){
            System.out.println("String[" + x + "][" + y + "]:" + value[x][y]);      
        }
    }
}
Dubas
+1  A: 

... just mixing too many languages to learn atm

Can I suggest that you need to stop and take the time to learn Java properly. Your code looks like you are trying to write "perlish" ... associative arrays and arrays instead of proper types. The end result is Java code that is slow and fragile compared with code that is designed and written using the Java mindset.

It might seem like you are being productive, but what you are producing is likely to be problematic going forward.

Stephen C