Java 如何为一个地图创建同一列表的多个实例?

Java 如何为一个地图创建同一列表的多个实例?,java,list,map,hashmap,Java,List,Map,Hashmap,假设我有一张地图和一张清单。如何为不同的键设置更多列表?我知道这个列表是通过一个引用来传递的,但是最接近这个引用的方法是什么呢 Map<Integer, List<Integer>> moves = new HashMap<Integer,List<Integer>>(); List<Integer> values = new LinkedList<Integer>(); //Populate th

假设我有一张地图和一张清单。如何为不同的键设置更多列表?我知道这个列表是通过一个引用来传递的,但是最接近这个引用的方法是什么呢

    Map<Integer, List<Integer>> moves = new HashMap<Integer,List<Integer>>();
    List<Integer> values = new LinkedList<Integer>();

    //Populate the map of moves to use it later for equation verification
    values.add(6);values.add(9);
    moves.put(0, values);
    values.clear();
    moves.put(1, values);
    values.add(3);
    moves.put(2,values);
    values.clear();
    values.add(2);values.add(5);
    moves.put(3, values);
    values.clear();
    moves.put(4, values);
    values.add(3);
    moves.put(5,values);
    values.clear();
    values.add(0);values.add(9);
    moves.put(6,values);
    values.clear();
    moves.put(7, values);
    moves.put(8, values);
    values.add(0);values.add(6);
    moves.put(9, values);
Map moves=newhashmap();
列表值=新建LinkedList();
//填充移动图,以便稍后用于等式验证
增加(6);增加(9);
移动。放置(0,值);
value.clear();
移动。放置(1,值);
增加(3);
移动。放置(2,值);
value.clear();
增加(2);增加(5);
移动。放置(3,值);
value.clear();
移动。放置(4,值);
增加(3);
移动。放置(5,值);
value.clear();
值。添加(0);增加(9);
移动。放置(6,值);
value.clear();
移动。放置(7,值);
移动。放置(8,值);
值。添加(0);增加(6);
移动。放置(9,值);

创建包含值的
数组列表的新实例

moves.put(0, new ArrayList<Integer>( values ));
moves.put(0,新数组列表(值));

这样,您就不必每次都将
引用与新对象相关联

创建包含值的
ArrayList
的新实例

moves.put(0, new ArrayList<Integer>( values ));
moves.put(0,新数组列表(值));

这样,您就不必每次都将
引用与新对象相关联

创建包含值的
ArrayList
的新实例

moves.put(0, new ArrayList<Integer>( values ));
moves.put(0,新数组列表(值));

这样,您就不必每次都将
引用与新对象相关联

创建包含值的
ArrayList
的新实例

moves.put(0, new ArrayList<Integer>( values ));
moves.put(0,新数组列表(值));

这样,您就不必每次都将
引用与新对象相关联

您必须创建一个新对象。否则,您将在所有键中放置相同的实例,并且当您从映射检索其值时,所有键都将具有相同的实例

Map<Integer, List<Integer>> moves = new HashMap<Integer,List<Integer>>();
List<Integer> values = new LinkedList<Integer>();

//Populate the map of moves to use it later for equation verification
values.add(6);values.add(9);
moves.put(0, values);
values = new LinkedList<Integer>();
values.add(8);
moves.put(1, values);
Map moves=newhashmap();
列表值=新建LinkedList();
//填充移动图,以便稍后用于等式验证
增加(6);增加(9);
移动。放置(0,值);
值=新的LinkedList();
增加(8);
移动。放置(1,值);

您必须创建一个新对象。否则,您将在所有键中放置相同的实例,并且当您从映射检索其值时,所有键都将具有相同的实例

Map<Integer, List<Integer>> moves = new HashMap<Integer,List<Integer>>();
List<Integer> values = new LinkedList<Integer>();

//Populate the map of moves to use it later for equation verification
values.add(6);values.add(9);
moves.put(0, values);
values = new LinkedList<Integer>();
values.add(8);
moves.put(1, values);
Map moves=newhashmap();
列表值=新建LinkedList();
//填充移动图,以便稍后用于等式验证
增加(6);增加(9);
移动。放置(0,值);
值=新的LinkedList();
增加(8);
移动。放置(1,值);

您必须创建一个新对象。否则,您将在所有键中放置相同的实例,并且当您从映射检索其值时,所有键都将具有相同的实例

Map<Integer, List<Integer>> moves = new HashMap<Integer,List<Integer>>();
List<Integer> values = new LinkedList<Integer>();

//Populate the map of moves to use it later for equation verification
values.add(6);values.add(9);
moves.put(0, values);
values = new LinkedList<Integer>();
values.add(8);
moves.put(1, values);
Map moves=newhashmap();
列表值=新建LinkedList();
//填充移动图,以便稍后用于等式验证
增加(6);增加(9);
移动。放置(0,值);
值=新的LinkedList();
增加(8);
移动。放置(1,值);

您必须创建一个新对象。否则,您将在所有键中放置相同的实例,并且当您从映射检索其值时,所有键都将具有相同的实例

Map<Integer, List<Integer>> moves = new HashMap<Integer,List<Integer>>();
List<Integer> values = new LinkedList<Integer>();

//Populate the map of moves to use it later for equation verification
values.add(6);values.add(9);
moves.put(0, values);
values = new LinkedList<Integer>();
values.add(8);
moves.put(1, values);
Map moves=newhashmap();
列表值=新建LinkedList();
//填充移动图,以便稍后用于等式验证
增加(6);增加(9);
移动。放置(0,值);
值=新的LinkedList();
增加(8);
移动。放置(1,值);


@AndrewThompson我不明白你在说什么mean@nachokk只有在你的提示下,当我读到我写的东西时,我才想到“我写这些东西到底是为了什么?真是胡说八道!”(胡说八道我的意思是胡说八道,而不仅仅是不正确的。)向所有人道歉,我不明白你说的是什么mean@nachokk只有在你的提示下,当我读到我写的东西时,我才想到“我写这些东西到底是为了什么?真是胡说八道!”(胡说八道我的意思是胡说八道,而不仅仅是不正确的。)向所有人道歉,我不明白你说的是什么mean@nachokk只有在你的提示下,当我读到我写的东西时,我才想到“我写这些东西到底是为了什么?真是胡说八道!”(胡说八道我的意思是胡说八道,而不仅仅是不正确的。)向所有人道歉,我不明白你说的是什么mean@nachokk只有在你的提示下,当我读到我写的东西时,我才想到“我写这些东西到底是为了什么?真是胡说八道!”(胡说八道我的意思是胡说八道,而不仅仅是不正确的。)向所有人道歉,对于噪音。@TurbutAlin我建议您使用
ArrayList
,而不是
LinkedList
。我从Reflection编写了
ArrayList
。为什么要使用ArrayList而不是LinkedList?由于LinkedList?@TurbutAlin为满足您的需要而使用了内存,因此您最好习惯于
列表的
ArrayList
实现。此外,在95%的情况下,使用了
ArrayList
。但是如果你真的很想了解细节,那么快速的谷歌搜索就可以了。:-)祝您好运。@TurbutAlin您可以阅读更多内容,但我不确定在此上下文中为什么要使用arrayList而不是linkedlist。@TurbutAlin顺便说一句,我建议您使用
arrayList
而不是
linkedlist
。我从Reflection编写了
ArrayList
。为什么要使用ArrayList而不是LinkedList?由于LinkedList?@TurbutAlin为满足您的需要而使用了内存,因此您最好习惯于
列表的
ArrayList
实现。此外,在95%的情况下,使用了
ArrayList
。但是如果你真的很想了解细节,那么快速的谷歌搜索就可以了。:-)祝你好运。@TurbutAl