关于List用法的一个小程序
2013-02-20 22:35
211 查看
import java.util.*; public class Deal { public static void main(String[] args) { if (args.length < 2) { System.out.println("Usage: Deal hands cards"); return; } int numHands = Integer.parseInt(args[0]); int cardsPerHand = Integer.parseInt(args[1]); // Make a normal 52-card deck. String[] suit = new String[] { "spades", "hearts", "diamonds", "clubs" }; String[] rank = new String[] { "ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king" }; List<String> deck = new ArrayList<String>(); for (int i = 0; i < suit.length; i++) for (int j = 0; j < rank.length; j++) deck.add(rank[j] + " of " + suit[i]); // Shuffle the deck. Collections.shuffle(deck); if (numHands * cardsPerHand > deck.size()) { System.out.println("Not enough cards."); return; } for (int i = 0; i < numHands; i++) System.out.println(dealHand(deck, cardsPerHand)); } public static <E> List<E> dealHand(List<E> deck, int n) { int deckSize = deck.size(); List<E> handView = deck.subList(deckSize - n, deckSize); List<E> hand = new ArrayList<E>(handView); handView.clear(); return hand; } }
相关文章推荐
- 一个关于list容器的小程序
- 关于一个普通程序启动另外一个程序的命令pm install
- 关于使用eclipse创建Android程序时会多出一个appcompat_v7
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- 【原创】关于程序卸载的一个Bug
- 关于一个多线程、断点续传的FTP服务程序!
- 关于Android的一个程序问题,货币转换
- 小程序大问题,MSDN中一个小小示例所带来的疑问,一个关于DataList的一个简单应用
- 一个简单的爬虫程序(爬取百度百科关于python的一千个页面)
- 关于EventHandlerList的用法简介
- 关于返回一个引用类型的函数的用法和见解
- C#学习小记5 一个有趣的程序关于复载property
- 关于ASP.NET中TreeView用法的一个小例子
- 一个关于位点的所在位置的程序
- 一个关于字符串统计的程序
- 一个关于多态的程序,有利于帮助学…
- 一个关于时间的故事(通过历史分析解决程序问题)
- 关于ExpandableListView用法的一个简单小例子
- 一个程序中关于多个osgGA::GUIEventHandler同时存在的问题
- 关于未能加载文件或程序集“Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项。试图加载格式不正确的程序的解决方案