有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何以指定的格式构造数据?

在项目的某个地方,我需要使用Java以如下格式构造数据 语法:

{String: [(int, int),(int, int),...]};

例如:

{"some expression": [(1, 20), (2,15),...]};

我试着使用地图收藏。不知何故,我能够将外部结构格式化为 但我很难在arraylist中格式化数据

    HashMap<String, ArrayList> expressionRanges = new HashMap<String, ArrayList>();

如何使用Java获得上述示例中指定的结构


共 (1) 个答案

  1. # 1 楼答案

    我想你可以用以下任何一种方法来做:

    Using Map with String as Key and 2D Integer Array as Value:

    // Declare the Map
    Map<String, int[][]> expressionRanges = new HashMap<>();
    
    // Declare the 2D Integer Arrays and fill them...
    int[][] a = {{1, 20}, {2,15}, {3,30}};
    int[][] b = {{6, 5}, {7,10}, {8,15}, {9,20},{10,25}};
    
    // Add Integer Arrays to Map as values 
    expressionRanges.put("Some Expression", a);
    expressionRanges.put("Other Expression", b);
    
    // Get 2D Integer Array from Map based on supplied Key string
    int[][] tmp = expressionRanges.get("Other Expression");
    // Display the contents of the 2D Integer Array
    for (int i = 0;i < tmp.length; i++) {
        for (int j = 0; j < tmp[i].length; j++) {
            System.out.print(tmp[i][j] + "\t");
        }
        System.out.println();
    }
    

    如果运行,则在控制台窗口中显示以下内容:

    6     5 
    7     10    
    8     15    
    9     20    
    10    25
    

    Using Map with String as Key and 2D ArrayList as Value:

    // Declare the Map
    Map<String, ArrayList<ArrayList<Integer>>> expressionRanges = new HashMap<>();
    
    // Declare the 2D ArrayLists of Integer Type and fill them...
    ArrayList<ArrayList<Integer>> a1 = new ArrayList<>();
        ArrayList<Integer> b1 = new ArrayList<>(); 
            b1.add(1); 
            b1.add(20); 
            a1.add(b1); 
        ArrayList<Integer> b2 = new ArrayList<>(); 
            b2.add(2); 
            b2.add(15); 
            a1.add(b2); 
        ArrayList<Integer> b3 = new ArrayList<>(); 
            b3.add(3); 
            b3.add(30); 
            a1.add(b3); 
    
    ArrayList<ArrayList<Integer>>a2 = new ArrayList<>();
        ArrayList<Integer> b4 = new ArrayList<>(); 
            b4.add(6); 
            b4.add(5); 
            a2.add(b4); 
        ArrayList<Integer> b5 = new ArrayList<>(); 
            b5.add(7); 
            b5.add(10); 
            a2.add(b5);  
        ArrayList<Integer> b6 = new ArrayList<>(); 
            b6.add(8); 
            b6.add(15); 
            a2.add(b6); 
        ArrayList<Integer> b7 = new ArrayList<>(); 
            b7.add(9); 
            b7.add(20); 
            a2.add(b7); 
        ArrayList<Integer> b8 = new ArrayList<>(); 
            b8.add(10); 
            b8.add(25); 
            a2.add(b8); 
    
    // Add ArrayLists to Map as values            
    expressionRanges.put("Some Expression", a1);
    expressionRanges.put("Other Expression", a2);                    
    
    // Get 2D ArrayList from Map based on supplied Key string
    ArrayList<ArrayList<Integer>> values =  expressionRanges.get("Other Expression");
    // Display the contents of the 2D ArrayList
    for(int i = 0; i < values.size(); i++){
        for(int j = 0; j < values.get(i).size(); j++){
            System.out.print(values.get(i).get(j) + "\t");
        }
        System.out.println();
    }
    

    如果运行,则在控制台窗口中显示以下内容:

    6     5 
    7     10    
    8     15    
    9     20    
    10    25