问答详情
源自:7-1 简易扑克牌游戏

请问Map用for循环添加后为什么只添加了最后一个对象

package game;

public class Paizus {

public int ID;

public String name;


public Paizus(int ID, String name) {

this.ID=ID;

this.name=name;

}

}


package game;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Set;


public class Sumgames{

Map<String,Paizus>newmap=new HashMap<String,Paizus>();


public void pukepai() {

Paizus[] pai= {new Paizus(0,"方片2"),new Paizus(1,"梅花2"),new Paizus(2, "黑桃2"),new Paizus(3, "红桃2"),

new Paizus(4,"方片3"),new Paizus(5,"梅花3"),new Paizus(6, "黑桃3"),new Paizus(7, "红桃3")};

for( int i1=0;i1<8;i1++) {

newmap.put("i1",pai[i1]);

}


System.out.println("总长为:"+newmap.size());

System.out.println("开始洗牌,洗牌后为:");

Set<String> keySet = newmap.keySet();

for (String string : keySet) {

Paizus paizu1=newmap.get(string);

System.out.println(paizu1.name);

}

}

public static void main(String[] args) {

Sumgames test=new Sumgames();

test.pukepai();

}

}


提问者:qq_onewayforwar_0 2017-08-25 21:59

个回答

  • qq_逆翔_0
    2018-05-03 16:44:00

    为什么不用putall呢

  • qq_AMicroera_0
    2017-08-27 01:27:24

    put是修改吧,add才是添加 remove是删除