Java 列表有问题吗

Java 列表有问题吗,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&g

我是一个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);
    }
}
===============


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

您需要在每次迭代中重新初始化映射。将初始化放入
for
循环中:

for(int x = 0; x < 2; x++) {
    HashMap<String,Object> childRowMap = new HashMap<String, Object>(); 
    ...
}
for(int x=0;x<2;x++){
HashMap childRowMap=新HashMap();
...
}

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

for(int x = 0; x < 2; x++) {
    HashMap<String,Object> childRowMap = new HashMap<String, Object>(); 
    ...
}
for(int x=0;x<2;x++){
HashMap childRowMap=新HashMap();
...
}

您添加了两次
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);
    }
}

您添加了两次
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);
    }
}

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

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);
List-actList=newarraylist();

对于(int x=0;x而言,您的代码中存在许多问题。可以按如下方式进行压缩:

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);
List-actList=newarraylist();

对于(intx=0;x,正如其他人所说,散列映射键应该是唯一的,最简单的修复方法是添加这一行

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

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

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

在for循环开始时,问题是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);
    }
}
class testMap{
公共静态void main(字符串[]args){
HashMap-childRowMap;
List actList=newarraylist();

对于(int x=0;x问题在于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);
    }
}
class testMap{
公共静态void main(字符串[]args){
HashMap-childRowMap;
List actList=newarraylist();

对于(intx=0;x映射,按键:值对存储。在您的代码中

childRowMap.put("startDate","startDate"+x)
它正在这样做:

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


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

映射按键:值对存储。执行此操作时在代码中

childRowMap.put("startDate","startDate"+x)
它正在这样做:

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


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

那么,这个例子比我的好得多。任何花时间绘制ASCII箭头以移动代码的人都有我的+1.)那么,这个例子比我的好得多。任何花时间画出ASCII箭头来移动代码的人都有我的+1:)最好(更安全的类型)将
actList
声明为
List
而不是
List
并相应地初始化它。你的
if(x==0){
else if(x==1){
块也是完全冗余的。去掉if/else,只保留其中一个的主体。这就是
for
循环的作用。最好(更安全的类型)将
actList
声明为
List
而不是
List
并相应地初始化它。您的
if(x==0){
else if(x==1){
块也是完全冗余的。去掉if/else,只保留其中一个的主体。这就是
for
循环的作用。