有点悲剧,做这题的时候总是有点搞不清楚到底用Collection 还是用Vector,刚开始用的Collection的时候总是不出结果,最后查API文档的时候才发现Collection中的remove()方法是删除指定元素的单个实例,弄明白一点了。
import java.util.*;
public class Num1 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
int keyNum;
Vector<Integer> vec = new Vector<Integer>();
Vector<Integer> vec1 = new Vector<Integer>();
for (int i = 1; i <= 13; i++)
vec.add(i);
while (vec.size() > 1) {
vec.add(vec.remove(0));
vec1.add(vec.remove(0));
}
vec1.add(vec.remove(0));
String[] str = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
int i = 0;
Iterator<Integer> it = vec1.iterator();
while (it.hasNext())
map.put((Integer) it.next(), str[i++]);
Set<Integer> set = map.keySet();
Iterator<Integer> it1 = set.iterator();
System.out.print(map.get(it1.next()));
while (it1.hasNext()) {
keyNum = (Integer) it1.next();
System.out.print("," + map.get(keyNum));
}
}
}
后来又找到一种简便的方法,贴出来吧
import java.util.ArrayList;
import java.util.Arrays;
public class 巧翻扑克牌 {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
ArrayList<String> ans = new ArrayList<String>();
a.addAll(Arrays.asList("K", "Q", "J", "10", "9", "8", "7", "6", "5",
"4", "3", "2", "A"));
while (a.size() != 0) {
ans.add(a.remove(0));
ans.add(ans.remove(0));
}
int i = ans.size() - 1;
for (; i > 0; i--)
System.out.print(ans.get(i) + ",");
System.out.println(ans.get(i));
}
}