请问如何用javaee实现学生座位表,就是将学生顺序打乱,放到指定的座位中去?
可以用数组实现!!!!!
无序的HashMap不知道能不能满足你的需求,本身是无序的,满足你的顺序打乱,指定的座位可以用固定的key-value对应,不确定能否帮助到你,如有不对请指出,谢谢!
典型的洗牌算法嘛
学生实体bean
package com.shuff.entity;
import java.io.Serializable;
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* student's userName
*/
private String username;
/**
* student's no
*/
private String no;
public Student(){
}
public Student(String username, String no) {
super();
this.username = username;
this.no = no;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
基于array的shuff操作
package com.shuff.logic;
import java.util.Random;
import com.shuff.entity.Student;
public class Shuffle {
// the total number of student
private static final int count = 50;
// the array of students
private Student[] students = new Student[count];
/**
* main function
*
* @param args
*/
public static void main(String[] args) {
Shuffle shuffle = new Shuffle();
shuffle.init();
shuffle.shffle();
shuffle.print();
}
/**
* initialize the array of students
*/
private void init() {
for (int index = 0; index < count; index++) {
students[index] = new Student("stu." + index, String.valueOf(index));
}
}
/**
* shuffle logic
*/
private void shffle() {
Random rd = new Random();
for (int i = 0; i < count; i++) {
int j = rd.nextInt(count);// 生成随机数
Student temp = students[i];// 交换
students[i] = students[j];
students[j] = temp;
}
}
private void print() {
for (Student item : students) {
System.out.println("[username,no] --> [" + item.getUsername() + "," + item.getNo() + "]");
}
}
}
基于ArrayList的shuffle操作
package com.shuff.logic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.shuff.entity.Student;
public class ShuffleList {
// the total number of student
private static final int count = 50;
// the array of students
private List<Student> students = new ArrayList<>();
/**
* main function
* @param args
*
*/
public static void main(String[] args){
ShuffleList shuffleList = new ShuffleList();
shuffleList.init();
shuffleList.shuffle();
shuffleList.print();
}
/**
* initialize the list of students
*/
private void init() {
for (int index = 0; index <= count; index++) {
students.add(new Student("stu." + index, String.valueOf(index)));
}
}
/**
* shuffle the list of student
*/
private void shuffle(){
Collections.shuffle(students);
}
/**
* print the list of the student
*/
private void print(){
for(Student item : students){
System.out.println("[username,no] --> [" + item.getUsername() + "," + item.getNo() + "]" );
}
}
}
座位看用list,学生用数组吧,座位编号是固定的,学生随机放
给每个学生一个编号,,按照座位总数逐一生成随机数就可以了