Java Hashmap 值到数组

发布于 2024-09-13 19:11:59 字数 927 浏览 3 评论 0原文

我有以下代码,它将一些数组添加到哈希图中,但随后我想访问这些数组以便稍后对它们进行一些工作。我已经走到这一步了,但无法弄清楚其余部分如何使其发挥作用......

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
            }
        }        

    }

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
            }
        }        

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-09-20 19:11:59

听起来像家庭作业,但有一些建议 -

  • 您的 Hashmap 没有理由采用 的形式 - 将其设为 ,因为这就是您要存储的内容。

你迭代了两次。你要么
- 迭代映射,无论是键、值还是条目。每个项目都是一个迭代器返回值,例如

for (String[][] s:map.values()){
 ...
}
  • hashmap.values.toArray 为您提供所有内容,这与迭代器正在执行的操作相同。

  • 如果您只是迭代内容,那么您并没有真正使用映射,因为您从未利用过您的值可通过键获取的事实。

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.

我不会写诗 2024-09-20 19:11:59

...只是混合了太多的语言来学习 atm

我可以建议你需要停止并花时间正确地学习Java。您的代码看起来像您正在尝试编写“perlish”...关联数组和数组而不是正确的类型。与使用 Java 思维方式设计和编写的代码相比,最终结果是 Java 代码缓慢且脆弱。

看起来你很有生产力,但你所生产的东西可能会在未来出现问题。

... 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.

谢绝鈎搭 2024-09-20 19:11:59

你的 while 循环应该是这样的 -

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

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

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);
                }
            }
        }
墨落画卷 2024-09-20 19:11:59

您的代码在这里格式不正确:

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

您尝试使用迭代器“迭代器”进行迭代,但接下来您调用

hashMap.values().toArray();

要获取循环的下一项,您需要使用 iterator.next();来获取它。也可以将“对象”更改为 String[][] 或 List。或列表<列表<字符串>>

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]);        
         }
        }
    }        
}

您还可以使用“foreach”循环来简化使用“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]);      
        }
    }
}

或者如果您只需要值:

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]);      
        }
    }
}

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]);      
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文