Storing a string and two doubles java

后端 未结 3 1794
野的像风
野的像风 2021-01-29 09:55

I have written a program which gives me three arrays.A string array and two dole arrays.... But I want to save them in one thing(I don\'t know if it would be an array or matrix)

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 10:19

    public static void main(String[] args) throws Exception {
    
        Map> theMap = new HashMap>();
    
        String [] fruits = {"Apple","Pear","Lemon"};
        Double [] firstDArray = {1.1,2.2,3.3};
        Double [] secondDArray = {11.11,22.22,33.33};
    
        for(int i = 0; i < fruits.length; i++){
            List innerList = new ArrayList();
            innerList.add(firstDArray[i]);
            innerList.add(secondDArray[i]);
            theMap.put(fruits[i], innerList);
        }
    
        for(Entry> en : theMap.entrySet()){
            System.out.print(en.getKey() + " : ");
            for(Double d : en.getValue()){
                System.out.print(d + " ");
            }
            System.out.println();
        }
    
    }
    

    In my example, I use a map corresponding to a list of numbers (doubles). The key to the map (string) is the string from the first array, and the list contains the numbers corresponding to the string from each other array. The above example gave me the output:

    Pear : 2.2 22.22 
    Lemon : 3.3 33.33 
    Apple : 1.1 11.11 
    

提交回复
热议问题