有 Java 编程相关的问题?

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

列表的java问题

我是一个java新手,对“java.util.List”有一些问题。下面是我的代码,我试图创建一个对象列表,但得到的结果并不理想。你能帮我解决这个问题吗

import java.util.*;

class  testMap
{
    public static void main(String[] args) 
    {


        HashMap<String,Object> childRowMap = new HashMap<String, Object>(); 
        List <Object> actList= new ArrayList <Object> ();

        for (int x=0;x<2 ;x++ ){

        if(x==0){

        childRowMap.put("startDate","startDate"+x);
        childRowMap.put("endDate","endDate"+x);
        childRowMap.put("encodedValue"," enc"+x);

        }
        else if (x==1){
        childRowMap.put("startDate","startDate"+x);
        childRowMap.put("endDate","endDate"+x);
        childRowMap.put("encodedValue"," enc"+x);
        }
        System.out.println("Adding object in the postition "+ x);
        actList.add(x,childRowMap);

        }
                System.out.println(actList);
    }
}

结果:

Adding object in the postition 0
Adding object in the postition 1
[{encodedValue= enc1, startDate=startDate1, endDate=endDate1}, {encodedValue= en
c1, startDate=startDate1, endDate=endDate1}]

===============

为什么我没有得到具有不同值的对象。请帮我解决代码的问题


共 (6) 个答案

  1. # 1 楼答案

    问题是childRowMap是在第一行代码中创建的同一个实例。当您执行for循环时,您不是在创建新实例,而是在现有HashMap中添加新值。这意味着ArrayList在执行引用检查时,会看到该对象已经在列表中,并且不会添加两次

    简单的修复方法:将初始化移到for循环中,这样在循环中的每次迭代中,HashMap都会被重新实例化

    class testMap { 
        public static void main(String[] args) {
            HashMap<String,Object> childRowMap;     
            List <Object> actList= new ArrayList <Object> ();    
    
            for (int x=0;x<2 ;x++ ){
                childRowMap = new HashMap<String, Object>();
                if(x==0){
                    childRowMap.put("startDate","startDate"+x);
                    childRowMap.put("endDate","endDate"+x);
                    childRowMap.put("encodedValue"," enc"+x);    
                } else if (x==1){
                    childRowMap.put("startDate","startDate"+x);
                    childRowMap.put("endDate","endDate"+x);
                    childRowMap.put("encodedValue"," enc"+x);    
                }
                System.out.println("Adding object in the postition "+ x);
                actList.add(x,childRowMap);
            }
            System.out.println(actList);
        }
    }
    
  2. # 2 楼答案

    正如其他人所说,散列映射键应该是唯一的,最简单的修复方法是添加这一行

    HashMap<String,Object> childRowMap = new HashMap<String, Object>(); 
    

    在for循环的开始

  3. # 3 楼答案

    地图按键:值对存储。当你这么做的时候

    childRowMap.put("startDate","startDate"+x)
    

    它正在这样做:

    childRowMap.put(Key,Value)
    

    因此,基本上,您将键startDate设置为等于startDate0(当x为0时)。在第二次循环时,将键startDate设置为等于startDate1(因为x是1)。现在,每当您查找startDate时,它将只存储startDate1,因为startDate0被覆盖,因为您只有一个映射

    可以通过在每次循环时重新初始化映射,或者对映射中输入的每个值使用新的唯一键来修复此问题

  4. # 4 楼答案

    你需要在每次迭代中重新初始化你的地图。将初始化放在for循环中:

    for(int x = 0; x < 2; x++) {
        HashMap<String,Object> childRowMap = new HashMap<String, Object>(); 
        ...
    }
    
  5. # 5 楼答案

    代码中有很多问题。所有这些都可以像这样压实:

    List <Map<String,Object>> actList= new ArrayList<Map<String,Object>> ();
    
    for (int x=0;x<2 ;x++ ){
        Map<String,Object> childRowMap = new HashMap<String, Object>();
        childRowMap.put("startDate","startDate"+x);
        childRowMap.put("endDate","endDate"+x);
        childRowMap.put("encodedValue"," enc"+x);
        System.out.println("Adding object in the postition "+ x);
        actList.add(childRowMap);
    }
    System.out.println(actList);
    

    其他几件事:

    • 作为java标准,您的类名应该从大写开始,所以考虑^ {CD1}},而不是^ {CD2}}
    • Map变量的类型应该是泛型的Map,而不是像HashMap这样的实现(我在回答中已经纠正了这一点)
    • 考虑将列表声明为更具体的类型List<Map<String,Object>>,而不是泛型对象,以确保类型安全
  6. # 6 楼答案

    您要添加childRowMap两次

    请注意,您正在将引用添加到childRowMap。这意味着从索引0和索引1的引用中都可以看到对地图的更改,这就是为什么看起来您添加了两个相同的对象

    您可以通过在循环中的每次迭代中创建一个新映射来修复它:

    import java.util.*;
    
    class testMap {
        public static void main(String[] args) {
    
    .-------
    |
    |       List<Object> actList = new ArrayList<Object>();
    |       
    |       for (int x = 0; x < 2; x++) {
    |           
    '---------> HashMap<String, Object> childRowMap = new HashMap<String, Object>();
    
                if (x == 0) {
                    childRowMap.put("startDate", "startDate" + x);
                    childRowMap.put("endDate", "endDate" + x);
                    childRowMap.put("encodedValue", " enc" + x);
                } else if (x == 1) {
                    childRowMap.put("startDate", "startDate" + x);
                    childRowMap.put("endDate", "endDate" + x);
                    childRowMap.put("encodedValue", " enc" + x);
                }
                System.out.println("Adding object in the postition " + x);
                actList.add(x, childRowMap);
    
            }
            System.out.println(actList);
        }
    }
    

    输出:

    Adding object in the postition 0
    Adding object in the postition 1
    [{encodedValue= enc0, startDate=startDate0, endDate=endDate0},
     {encodedValue= enc1, startDate=startDate1, endDate=endDate1}]