java 地图点放置在 for 循环中不起作用

java map dot put not working in a for loop

我是地图的新手 class,我想知道我做错了什么。下面是我正在努力学习地图的 class。

import java.util.*;  
public class Maptester{
  Map<Integer, String> map = new HashMap<Integer, String>();
  public Maptester(String[] x){
    for(int i = 0; i > x.length; i++) map.put(i, x[i]);
  }
  public Maptester(ArrayList<String> x){
    for(int i = 0; i > x.size(); i++) map.put(i, x.get(i));
  }
  public String toString(){
    String x = "";
    for(Map.Entry m:map.entrySet()){  
      x += (m.getKey()+" "+m.getValue()+"\n");
    } return x; 
  }
}

这是我正在使用的主要 Class。

import java.util.*;
class Main {
  public static void main(String[] args) {
    String[] x = {"x", "y", "z"};
    Maptester b = new Maptester(x);
    System.out.print(b);
  }
}

这个输出什么都没有,出于某种原因,顶部的 for 循环中的 map 中没有任何内容,我不明白为什么。

您正在检查 for 循环中的错误谓词。这将完成工作:

public class Maptester{
  Map<Integer, String> map = new HashMap<Integer, String>();
  public Maptester(String[] x){
    for(int i = 0; i < x.length; i++) map.put(i, x[i]);
  }
  public Maptester(ArrayList<String> x){
    for(int i = 0; i < x.size(); i++) map.put(i, x.get(i));
  }
  public String toString(){
    String x = "";
    for(Map.Entry m:map.entrySet()){  
      x += (m.getKey()+" "+m.getValue()+"\n");
    } return x; 
  }
}

在 for 循环中,您输入了错误的比较运算符“>”而不是“<”。正确的代码是:

public Maptester(String[] x){
    for(int i = 0; i < x.length; i++) map.put(i, x[i]);
}