English 中文(简体)
将散图转换成队列
原标题:Converting HashMap into Array
  • 时间:2012-05-26 09:17:58
  •  标签:
  • java
  • android

I have a HashMap which I want to convert into a array. Which the code below, I get following: [[2, 11, 10, 9], [value1, value2, value3, value4], [null, null, null, null], [null, null, null, null]] The "null" entries give me a null pointer exception. Why is it two times as big as it should be? I just want the real entries: [[2, 11, 10, 9], [value1, value2, value3, value4]]. What did I wrong?

String[][] test = getArrayFromHash(hashmap);

    public static String[][] getArrayFromHash(HashMap<String, String> hashMap){
        String[][] str = null;
        {
            Object[] keys = hashMap.keySet().toArray();
            Object[] values = hashMap.values().toArray();
            str = new String[keys.length][values.length];
            for(int i=0;i<keys.length;i++) {
                str[0][i] = (String)keys[i];
                str[1][i] = (String)values[i];
            }
        }
        return str;
    }

谢谢!

最佳回答

参考link ::

    String[][] arr = new String[hashmap.size()][2];
    Set entries = hashmap.entrySet();
    Iterator entriesIterator = entries.iterator();

    int i = 0;
    while(entriesIterator.hasNext()){

        Map.Entry mapping = (Map.Entry) entriesIterator.next();

        arr[i][0] = mapping.getKey().toString();
        arr[i][1] = mapping.getValue().toString();

        i++;
    }

< 强度 > 更新日期: : 以获得两个 1D 数组

String[] arr1 = new String[hashmap.size()];
String[] arr2 = new String[hashmap.size()];
        Set entries = hashmap.entrySet();
        Iterator entriesIterator = entries.iterator();

        int i = 0;
        while(entriesIterator.hasNext()){

            Map.Entry mapping = (Map.Entry) entriesIterator.next();

            arr1[i] = mapping.getKey().toString();
            arr2[i] = mapping.getValue().toString();

            i++;
        }
问题回答

分配适当大小的阵列

str = new String[2][values.length];

此外,您应该主张数值和键的长度相同。同样,匹配条目的位置也相同。类似的东西。

String[] keys = hashMap.keySet().toArray(new String[0]);
String[] values = new String[keys.length];
String[][] str = {keys, values};
for(int i=0;i<keys.length;i++) values[i] = hashMap.get(keys[i]);
return str;

应做(未试验过),

我不确定键集和值是否有必要以相同顺序返回值(执行文档中没有具体说明,所以我说它没有保证)。

尝试与条目集一起工作 :

Set<Map.Entry<String, String>> set = hm.entrySet();
int i = 0;
for (Map.Entry<String,String> me : set) {
  arr[i][0] = me.getKey();
  arr[i][1] = me.getValue()};
  i++;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签