arraylist - 我们如何在 HashMap 中的 ArrayList 中进行迭代?
问题描述
我想分别打印 ArrayList 中的每个值,即 {1=[A, B,C, D], 2=[E, F, G, H]}
哈希映射>哈希=新哈希映射>(); // 现在我想遍历 HashMap 中特定键的数组列表
如果用户输入 1(即 Key),则输出应为 A B C D
如果用户输入 2(即 Key),则输出应为 E F G H
解决方案
我不太确定,我得到了你的问题,但是如果你想遍历一个键数组并在哈希图中查找与这些键对应的所有值,你可以做这样的事情(假设问题在 java 中) :
import java.util.*;
class Main {
public static HashMap<Integer, String[]> hmap = new HashMap<Integer, String[]>();
public static int[] arrToTraverse = {1,2};
public static void main(String[] args) {
String[] s1 = {"A", "B", "C", "D"};
String[] s2 = {"E", "F", "G", "H"};
hmap.put(1, s1);
hmap.put(2, s2);
for(int no : arrToTraverse) {
System.out.println(Arrays.toString(getValue(no)));
}
}
public static String[] getValue(int key) {
return hmap.get(key);
}
}
这将输出:
[A, B, C, D]
[E, F, G, H]
推荐阅读
- javascript - 在猫头鹰旋转木马中设置旋转木马高度
- java - 在 Spring Security 中使用 UserDetails 实现登录不起作用
- r - 如何在绘图上写出均值、最小值、最大值、中值、P 值和安德森-达林测试结果的结果
- r - 在R中使用udpipe提取关键字时的for循环
- android - 为什么这个分页库示例的性能比仅仅一个回收器视图差得多?
- android - Lenovo Smart Display 或 Google Home Hub 上的 Android Things?
- java - 是否可以开发一个 Android 服务来准确查看其他应用程序打开了哪个视图?
- php - pdo_sqlanywhere macOS 莫哈韦
- java - 对象数组空检查
- java - 如何在for循环中取前2个元素然后跳过第三个元素java