package com.baidu.map;
济源网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联建站成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestMap02 {
public static void main(String[] args) {
ListStudent list=new ArrayListStudent();
exam(list);
MapString, ClassRoom rooms=new HashMapString, ClassRoom();
count(rooms, list);
printScore(rooms);
}
/**
* 打印
*/
private static void printScore(MapString, ClassRoom rooms) {
SetMap.EntryString, ClassRoom entrySet=rooms.entrySet();
IteratorMap.EntryString, ClassRoom it=entrySet.iterator();
while(it.hasNext()){
Map.EntryString, ClassRoom entry=it.next();
ClassRoom room=entry.getValue();
System.out.println("班号:"+room.getNo()+"总成绩:"+room.getTotal()+"平均成绩:"+room.getTotal()/room.getStus().size());
}
}
/**
* 统计班级的分数
*/
public static void count(MapString, ClassRoom rooms,ListStudent list){
for (Student student : list) {
ClassRoom room=rooms.get(student.getNo());
if(null==room){
room=new ClassRoom(student.getNo());
rooms.put(student.getNo(), room);
}
room.setTotal(room.getTotal()+student.getScore());
room.getStus().add(student);
}
}
/**
* 数据源
*/
public static void exam(ListStudent list){
//可以使用循环遍历进去
list.add(new Student(80, "a", "001"));
list.add(new Student(80, "b", "001"));
list.add(new Student(80, "c", "001"));
list.add(new Student(80, "d", "001"));
list.add(new Student(80, "e", "002"));
list.add(new Student(80, "f", "002"));
list.add(new Student(80, "g", "002"));
list.add(new Student(80, "h", "002"));
list.add(new Student(80, "i", "003"));
list.add(new Student(80, "j", "003"));
list.add(new Student(80, "k", "003"));
list.add(new Student(83, "l", "003"));
}
}
package com.baidu.map;
public class Student {
private double score;
private String name;
private String no;
public Student() {
}
public Student(double score, String name, String no) {
super();
this.score = score;
this.name = name;
this.no = no;
}
@Override
public String toString() {
return "Student [score=" + score + ", name=" + name + ", no=" + no
+ "]";
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
package com.baidu.map;
import java.util.ArrayList;
import java.util.List;
public class ClassRoom {
private String no;
private double total;
private ListStudent stus;
public ClassRoom() {
stus=new ArrayListStudent();
}
public ClassRoom(String no) {
this();
this.no = no;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public ListStudent getStus() {
return stus;
}
public void setStus(ListStudent stus) {
this.stus = stus;
}
}
以前的!
====代码====
package com;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 网球赛
* @author Cris
* @ClassName: Match
* @Version
* @ModifiedBy
* @Copyright @ 2010 HL Technology Inc.
* @date 2011-5-24 上午11:29:39
* @description
*/
public class Match implements ActionListener{
private JFrame f;
private JTextField tf;
private JPanel container;
private JList myList ;
private JLabel message;
private JButton b;
/**
* @author Cris
* @title: main
* @date 2011-5-23 上午11:07:27
* @param args void
*/
public static void main(String[] args) {
Match t = new Match();
t.go();
}
public void go(){
f = new JFrame("比赛");
f.setLocation(100, 200);
f.setSize(600,500);
container = new JPanel(new BorderLayout());
f.setContentPane(container);
JPanel title = new JPanel(new FlowLayout());
tf = new JTextField(10);
title.add(tf);
b = new JButton("确定");
b.addActionListener(this);
title.add(b);
message = new JLabel();
title.add(message);
container.add(title,BorderLayout.NORTH);
myList = new JList();
container.add(myList,BorderLayout.CENTER);
f.setVisible(true);
}
/**
* 显示比赛结果
* @author Cris
* @title: show
* @date 2011-5-24 上午11:31:05
* @param n void
*/
private void show(int n){
Cris c = new Cris(n);
ListListString[] result = c.getResult();
String[] str = new String[result.size()];
for (int i = 0; i result.size(); i++) {
StringBuilder sb = new StringBuilder();
sb.append("第").append(i+1).append("天: ");
ListString[] matchList = result.get(i);
for(String[] match : matchList){
sb.append("[").append(match[0]).append("-").append(match[1]).append("] ");
}
str[i] = sb.toString();
}
myList.setListData(str);
message.setText("");
f.setVisible(true);
}
/**
* 清空数据
* @author Cris
* @title: clear
* @date 2011-5-24 上午11:30:36 void
*/
private void clear(){
String[] str = {""};
myList.setListData(str);
message.setText("");
}
public void actionPerformed(ActionEvent e) {
clear();
String s = tf.getText();
if (s == null || "".equals(s = s.trim()) ) {
message.setText("请输入一个偶数");
}
try {
int n = Integer.parseInt(s);
if (n = 0 || n % 2 != 0) {
message.setText("请输入一个偶数");
}else{
show(n);
}
} catch (Exception ex) {
message.setText("请输入一个偶数");
}
f.setVisible(true);
}
}
第二个类:
package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Cris {
/**选手数量*/
private int n ;
/**各个运动员还没进行的比赛 key:球员名称,value:需进行的比赛集合*/
private MapString, ListString[] playerMacth ;
/**球员人名*/
private String[] players ;
/**最终每天的比赛分布集合*/
private ListListString[] result;
/**所有可选的比赛*/
private ListListString[] allMatchData[];
/**每天的比赛*/
ListString[] dayMatch ;
/**一个随机对象*/
private Random r;
/**当前运算到的天数*/
private int nowDay;
public Cris(int n){
this.n = n;
playerMacth = new HashMapString, ListString[]();;
result = new ArrayListListString[]();
allMatchData = new LinkedList[n-1];
r = new Random();
init();
go();
}
/**
* 初始化球员
* @author Cris
* @title: init
* @date 2011-5-23 下午04:22:32 void
*/
private void init(){
//定义球员人名,以数字代表,从1开始
players = new String[n];
for (int i = 0; i players.length; i++) {
//定义球员的名字
players[i] = String.valueOf(i+1);
//存放每个球员要进行的比赛的集合
ListString[] matchList = new LinkedListString[]();
//球员赛事集合
playerMacth.put(players[i], matchList);
//球员集合,不变
// playerListConst.add(players[i]);
}
//定义每个人的比赛
for (int i = 0; i players.length -1; i++) {
for (int j = i+1; j players.length ; j++) {
String[] match = {players[i],players[j]};//定义一个比赛
//把此赛事添加到相应球员需比赛集合中
playerMacth.get(players[i]).add(match);
playerMacth.get(players[j]).add(match);
}
}
}
/**
* 随机获得一个当前天数可进行的比赛组合,并把此比赛组合从总集合中移除
* 如果当前天数无比赛,则先获得比赛,再随机
* @author Cris
* @title: getNowDayMatch
* @date 2011-5-24 上午09:48:23
* @return ListString[]
*/
private ListString[] getNowDayMatch(){
if (allMatchData[nowDay] == null) {
setAllMatchNowDay();
}
if (! allMatchData[nowDay].isEmpty()) {
int index = r.nextInt(allMatchData[nowDay].size());
ListString[] list = allMatchData[nowDay].get(index);
allMatchData[nowDay].remove(index);
return list;
}else {
return null;
}
}
/**
* 取消前一天的比赛
* @author Cris
* @title: cancelYesterday
* @date 2011-5-24 上午09:56:12 void
*/
private void cancelYesterday(){
//清空当前天数的可选比赛组合
allMatchData[nowDay] = null ;
//时间恢复到前一天
nowDay--;
//获得前一天的比赛
ListString[] matchList = result.get(nowDay);
//取消前一天的比赛
result.remove(nowDay);
//恢复每个运动员未进行的比赛
for(String[] match : matchList){
playerMacth.get(match[0]).add(match);
playerMacth.get(match[1]).add(match);
}
}
public void go(){
//循环比赛天数
for ( nowDay = 0; nowDay n -1; ) {
//1.随机一个比赛
ListString[] matchList = getNowDayMatch();
//无比赛组合 ,返回上一天 取消上一天的比赛,时间回退到前一天,跳过此次循环
if (matchList == null) {
cancelYesterday();
continue;
}
//3遍历比赛 去除运动员 去除运动员的未进行比赛
for(String[] match : matchList){
playerMacth.get(match[0]).remove(match);
playerMacth.get(match[1]).remove(match);
}
result.add(matchList);
nowDay++;
}
}
/**
* 从1号队员开始,获得当前天数所有可组合的赛事
* @author Cris
* @title: setAllMatchNowDay
* @date 2011-5-24 上午09:42:51 void
*/
private void setAllMatchNowDay(){
allMatchData[nowDay] = new LinkedListListString[]();
int n = 1;//1号运动员
//此运动员未进行的比赛
ListString[] matchList = playerMacth.get(String.valueOf(n));
for(String[] match : matchList){
ListString[] selectedList = new ArrayListString[]();
selectedList.add(match);
doDetAllMatchNowDay(n+1, selectedList);
}
}
/**
* 今天比赛的所有组合
* @author Cris
* @title: doDetAllMatchNowDay
* @date 2011-5-24 上午09:46:05
* @param playerNum
* @param selectedList void
*/
private void doDetAllMatchNowDay(int playerNum,ListString[] selectedList){
//运动员名称
String player = String.valueOf(playerNum);
boolean flag = true;
//判断此运动员是不是已被选过
for(String[] match : selectedList){
if (match[0].equals(player) || match[1].equals(player)) {
flag = false;
break;
}
}
//已被选过
if ( flag == false) {
//最后一个人员
if (playerNum == n) {
allMatchData[nowDay].add(selectedList);
}else{
doDetAllMatchNowDay(playerNum+1, selectedList);
}
return;
}
//此运动员未进行的比赛
ListString[] matchList = playerMacth.get(player);
//此运动员可选择的比赛
ListString[] canDoList = new ArrayListString[]();
for(String[] match : matchList){
flag = true;
for(String[] selected : selectedList){
if(match[0].equals(player)){
if(match[1].equals(selected[0])){
flag = false;
}else if(match[1].equals(selected[1])){
flag = false;
}
}else{
if(match[0].equals(selected[0])){
flag = false;
}else if(match[0].equals(selected[1])){
flag = false;
}
}
}
if (flag) {
canDoList.add(match);
}
}
//遍历此运动员可选择的比赛
for(String[] match : canDoList){
ListString[] newList = copyArrayList(selectedList);
//记录当前比赛
newList.add(match);
//最后一个人员
if (playerNum == n) {
allMatchData[nowDay].add(newList);
}else{
doDetAllMatchNowDay(playerNum+1, newList);
}
}
}
/**
* 复制集合
* @author Cris
* @title: copyArrayList
* @date 2011-5-24 上午09:24:44
* @param list
* @return ListString[]
*/
private ListString[] copyArrayList(ListString[] list){
ListString[] newList = new ArrayListString[]();
for(String[] s : list){
newList.add(s);
}
return newList;
}
public ListListString[] getResult() {
return result;
}
}
现在没有单纯的java的比赛了,我参加过的就是非要是一些网站类的比赛,当然也不必要求用java。
在网站方面就是对于j2ee的学习了,这种比赛需要对数据处理的能力,比如java封转好的一些数据类型ArrayList或者哈希表之类,都要会用。
单纯从java语言本身来看的话,你首先要对面向对象有一个认识,其实就是java中的抽象机制,接口和继承。你还要了解java的内存机制,比如其著名的垃圾回收机制。
最后一个进阶的东西就是多线程,这是一个编程永恒的主题,并且java对于多线程支持的非常的好。如果以上java的知识你都掌握,就可以看看设计模式了,可以提高能力。当然也可以读java的源码
经验大概就是这些,比赛的一个关键是你有足够的技术积累,不单单是你的代码量,还有总结和自己的认识。
其实比赛而言,看的就是谁把知识理解的深刻,不要总是停留在表面,祝你取得好成绩。