请问如何用javaee实现学生座位表,就是将学生顺序打乱,放到指定的座位中去?

请问如何用javaee实现学生座位表,就是将学生顺序打乱,放到指定的座位中去?

5个回答

可以用数组实现!!!!!

无序的HashMap不知道能不能满足你的需求,本身是无序的,满足你的顺序打乱,指定的座位可以用固定的key-value对应,不确定能否帮助到你,如有不对请指出,谢谢!

p577460400
勿忘初心0_0 可以有详细的代码吗?
3 年多之前 回复

典型的洗牌算法嘛

学生实体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() + "]" );
        }
    }
}





qq_42239934
qq_42239934 回复勿忘初心0_0: 请问你是怎么做的,有源代码参考一下吗
6 个月之前 回复
p577460400
勿忘初心0_0 我是基于Spring MVC开发的,需要从MySql数据库获取数据,封装成一个对象(学号、姓名、座位号),将学生打乱放到一个二维数组中,然后前台JSP页面按照座位号的顺序显示学生座位表
3 年多之前 回复

座位看用list,学生用数组吧,座位编号是固定的,学生随机放

p577460400
勿忘初心0_0 可以有详细的代码吗?
3 年多之前 回复
p577460400
勿忘初心0_0
3 年多之前 回复

给每个学生一个编号,,按照座位总数逐一生成随机数就可以了

p577460400
勿忘初心0_0 可以有详细的代码吗?
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!