我想如果有人可以签出这段代码,那么代码应该将数组转换为列表,然后转换为地图。键应具有list元素的值,如果它是偶数,则值应为True,如果是奇数,则值应为False。数组为“ 8、6、1、5、3、9、2”。我很确定这是正确的方法,但是在打印地图时,我得到很多线条,这是我第一次使用地图,因此我不确定是否应该这样做,否则我会弄乱某些东西。代码:
static public void toMap(int x[]) {
List<Integer> list = new ArrayList<>();
Map<Integer, String> map = new HashMap<>();
for (int t : x) {
list.add(t);
}
for(int z : list) {
String tf;
if(z % 2 == 0)
tf = "True";
else
tf = "False";
map.put(z,tf);
for (Map.Entry<Integer, String> mp : map.entrySet()) {
System.out.println(mp.getKey() + "/" + mp.getValue());
}
}
}
在打印时获取此信息:
任何帮助/提示,将不胜感激!
答案 0 :(得分:3)
您正在for循环中进行打印,这是导致问题的原因。您需要将其移至for循环之外。这是更新的代码-
static public void toMap(int[] x) {
List<Integer> list = new ArrayList<>();
for (int t : x) {
list.add(t);
}
Map<Integer, String> map = new HashMap<>();
for(int z : list) {
String tf;
if(z % 2 == 0)
tf = "True";
else
tf = "False";
map.put(z,tf);
}
for (Map.Entry<Integer, String> mp : map.entrySet()) {
System.out.println(mp.getKey() + "/" + mp.getValue());
}
}
您还可以将其简化如下-
public static void main(String[] args) {
Integer[] array = {8, 6, 1, 5, 3, 9, 2};
toMap(array);
}
static public void toMap(Integer[] x) {
List<Integer> list = Arrays.asList(x);
Map<Integer, String> map = new HashMap<>();
list.forEach(i -> {
if (i % 2 == 0)
map.put(i, "True");
else
map.put(i, "False");
});
System.out.println(map);
}
答案 1 :(得分:2)
您正在将用于打印地图的循环放入创建地图的循环中,因此,每次添加条目时,地图都会重新打印地图。实际上,您确实具有正确的地图-输出的最后几行。
要解决此问题,您应该将最后一个for loop移出以制作
for(int z : list) {
String tf;
if(z % 2 == 0)
tf = "True";
else
tf = "False";
map.put(z,tf);
}
for (Map.Entry<Integer, String> mp : map.entrySet()) {
System.out.println(mp.getKey() + "/" + mp.getValue());
}
答案 2 :(得分:1)
这是一个清楚的示例,说明了为什么总是将花括号放在代码中会迷路。
for(int z : list) {
String tf;
if(z % 2 == 0) {
tf = "True";
}
else {
tf = "False";
}
map.put(z,tf);
for (Map.Entry<Integer, String> mp : map.entrySet()) {
System.out.println(mp.getKey() + "/" + mp.getValue());
}
}
如果我们将花括号放在此处,则可以清楚地看到在另一张里面有印刷for-loop
。没有花括号,这很难看。
将该打印循环移到另一个循环之外,您应该会很好。
答案 3 :(得分:1)
您需要将打印循环移到第一个循环之外。
还有一些观察结果。
true
或false
。那么为什么不执行以下操作: for(int z : list) { // you can also replace list with your array here
map.put(z,z % 2 == 0);
}
// or if you want Strings
for (int z : list) {
map.put(z, z % 2 == 0 ? "True":"False");
}
map.forEach((k,v)->System.out.println(k + "/" + v));
答案 4 :(得分:0)
您不需要在数组上迭代两次。仅重复一次,将其添加到列表中,然后将其添加到映射中的偶数或奇数。
static public void toMap(int x[]) {
// List which contains all elements of x.
List<Integer> list = new ArrayList<>();
// Map to store element as key and even or not as value
Map<Integer, Boolean> map = new HashMap<>();
for (int value: x) {
list.add(value);
map.put(value, value % 2 == 0);
}
// Printing the map
for (Map.Entry<Integer, Boolean> m : map.entrySet()) {
System.out.println(m.getKey() + "," + m.getValue());
}
}
答案 5 :(得分:0)
正如其他人所说,您的问题出在嵌套中。您正在遍历输入数组中每个整数的所有映射条目。就是说,我认为更大的问题是您使方法过于复杂。使用一些技巧,您可以将代码简化为以下形式:
public static void toMap(int[] x) {
HashMap<Integer, String> map = new HashMap<>();
ArrayList<Integer> list = new ArrayList<>();
for (int i : x) {
map.put(i, (i & 1) == 0 ? "True" : "False"); //puts "True" if i is even, else "False"
}
list.addAll(map.keySet()); //adds all the map's keys to the ArrayList
map.forEach((k, v) -> System.out.printf("%d/%s%n", k, v)); //prints out all the entries formatted as you specified
}
或者,如果实际上不需要特定的ArrayList,则可以摆脱它:
public static void toMap(int[] x) {
HashMap<Integer, String> map = new HashMap<>();
for (int i : x) {
map.put(i, (i & 1) == 0 ? "True" : "False"); //puts "True" if i is even, else "False"
}
map.forEach((k, v) -> System.out.printf("%d/%s%n", k, v)); //prints out all the entries formatted as you specified
}
或者,如果您实际上不需要地图的任何功能,也可以摆脱它。只需从for循环中直接打印即可,就像这样:
public static void toMap(int[] x) {
for (int i : x) {
System.out.printf("%d/%s%n", i, (i & 1) == 0 ? "True" : "False");
}
}
或者,如果您不介意小写的true / false,则可以让Java为您完成字符串转换:
public static void toMap(int[] x) {
for (int i : x) {
System.out.printf("%d/%s%n", i, (i & 1) == 0);
}
}
最后,如果您喜欢单线,并且不介意它有点凌乱,则可以一行完成所有操作:
public static void toMap(int[] x) {
IntStream.of(x).forEach((i) -> System.out.printf("%d/%s%n", i, (i & 1) == 0));
}