我有以下代码:
import java.util.Arrays;
public class Puke {
String[] pukeNum = new String[13];
String pukeFlower;
public Puke( String pukeFlower,String[] pukeNum) {
super();
this.pukeNum = pukeNum;
this.pukeFlower = pukeFlower;
}
@Override
public String toString() {
return "Puke [pukeNum=" + Arrays.toString(pukeNum) + ", pukeFlower="
+ pukeFlower + "]";
}
main类代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class PukePlay {
/**
* @param args
*/
public static void main(String[] args) {
Map<String,Puke> map = new HashMap<String,Puke>();
map.put("1",new Puke("黑桃",new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}));
map.put("2",new Puke("红桃",new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}));
map.put("3",new Puke("草花",new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}));
map.put("4",new Puke("方块",new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}));
for(int i = 1; i <=map.size() ; i++){
System.out.println(map.get(""+i).pukeFlower+":"+map.get(""+i).pukeNum );
}
Set<String> s = map.keySet();
String str[] = new String[13];
}
}
}
5 个解决方案
#1
String[] = map.get("key的名字");
#2
int i1[] = (int[]) retMap.get(j);
Log.i(String.valueOf(j), String.valueOf(i1[0]));
报错:java.lang.NullPointerException: Attempt to read from null array
#3
Set<Entry<String, Object>> set =map.entrySet();
Iterator<Entry<String, Object>> it = set.iterator();
while(it.hasNext()){
Entry<String, Object> entry = it.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
Iterator<Entry<String, Object>> it = set.iterator();
while(it.hasNext()){
Entry<String, Object> entry = it.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
#4
我觉的你这个扑克是结构设计的有问题.
public class Puke {
String pukeNum;
String pukeFlower;
public Puke(String pukeNum, String pukeFlower) {
this.pukeFlower = pukeFlower;
this.pukeNum = pukeNum;
}
}
//初始化一副扑克
public List<Puke> init() {
List<Puke> one = new ArrayList<>();
for (int i = 0; i < 4; i++) {
String pukeFlower = null;
switch (i) {
case 0:
pukeFlower = "黑桃";
break;
case 1:
pukeFlower = "红桃";
break;
case 2:
pukeFlower = "草花";
break;
case 3:
pukeFlower = "方块";
break;
}
for (int j = 0; j < 13; j++) {
String pukeNum = null;
switch (i) {
case 0:pukeNum = "A";break;
case 1:pukeNum = "1";break;
case 2:pukeNum = "2";break;
case 3:pukeNum = "3";break;
case 4:pukeNum = "4";break;
case 5:pukeNum = "5";break;
case 6:pukeNum = "6";break;
case 7:pukeNum = "7";break;
case 8:pukeNum = "8";break;
case 9:pukeNum = "9";break;
case 10:pukeNum = "J";break;
case 11:pukeNum = "Q";break;
case 12:pukeNum = "K";break;
}
Puke puke =new Puke(pukeNum, pukeFlower);
one.add(puke);
}
}
one.add(new Puke("", "King"));
one.add(new Puke("", "Wang"));
return one;
}
#5
修改下
#1
String[] = map.get("key的名字");
#2
int i1[] = (int[]) retMap.get(j);
Log.i(String.valueOf(j), String.valueOf(i1[0]));
报错:java.lang.NullPointerException: Attempt to read from null array
#3
Set<Entry<String, Object>> set =map.entrySet();
Iterator<Entry<String, Object>> it = set.iterator();
while(it.hasNext()){
Entry<String, Object> entry = it.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
Iterator<Entry<String, Object>> it = set.iterator();
while(it.hasNext()){
Entry<String, Object> entry = it.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
#4
我觉的你这个扑克是结构设计的有问题.
public class Puke {
String pukeNum;
String pukeFlower;
public Puke(String pukeNum, String pukeFlower) {
this.pukeFlower = pukeFlower;
this.pukeNum = pukeNum;
}
}
//初始化一副扑克
public List<Puke> init() {
List<Puke> one = new ArrayList<>();
for (int i = 0; i < 4; i++) {
String pukeFlower = null;
switch (i) {
case 0:
pukeFlower = "黑桃";
break;
case 1:
pukeFlower = "红桃";
break;
case 2:
pukeFlower = "草花";
break;
case 3:
pukeFlower = "方块";
break;
}
for (int j = 0; j < 13; j++) {
String pukeNum = null;
switch (i) {
case 0:pukeNum = "A";break;
case 1:pukeNum = "1";break;
case 2:pukeNum = "2";break;
case 3:pukeNum = "3";break;
case 4:pukeNum = "4";break;
case 5:pukeNum = "5";break;
case 6:pukeNum = "6";break;
case 7:pukeNum = "7";break;
case 8:pukeNum = "8";break;
case 9:pukeNum = "9";break;
case 10:pukeNum = "J";break;
case 11:pukeNum = "Q";break;
case 12:pukeNum = "K";break;
}
Puke puke =new Puke(pukeNum, pukeFlower);
one.add(puke);
}
}
one.add(new Puke("", "King"));
one.add(new Puke("", "Wang"));
return one;
}
#5
修改下