java做一个值日生表。要用到链表,random,list。一共十三个人,四个人一组,最后剩下一个自己一个集合。不能重复

package com.wanju.project001.zonghe.test;


import java.util.LinkedList;
import java.util.List;
import java.util.Random;


public class TestWork {


public static final int ALL_NUM = 13;
List<Student> ls = new LinkedList<Student>();
List<Student> lsForCopy = new LinkedList<Student>();
List<LinkedList<Student>> lss = new LinkedList<LinkedList<Student>>();


public void init() {
for (int i = 0; i < ALL_NUM; i++) {
Student s = new Student();
s.setNum(i);
s.setName("s" + i);
ls.add(s);
}
}


public static void main(String[] args) {
TestWork t = new TestWork();
t.init();
t.group();
t.show();
}


public void group() {
for (int i = 0; i < 4; i++) {
lss.add(new LinkedList<Student>());
}
boolean flg = true;
Random r = new Random();
StudentHelper helper = new StudentHelper(ls);
helper.setLsForCopy(lsForCopy);
for (int k = 0; k < 4; k++) {


for (int i = 0; i < 4; i++) {


while (flg) {
int v = r.nextInt(ls.size());
if (!helper.checkStudentByNum(v)) {
Student ss = helper.getStudentByNum(v);
lss.get(k).add(ss);
lsForCopy.add(ss);
helper.addLsForCopyStudent(ss);
if (lss.get(k).size() >= 4) {
flg = false;
}
if (k == 3) {
flg = false;
}
break;
}
}
}
flg = true;
}
}


public void show() {
for (int i = 0; i < lss.size(); i++) {
System.out.println(lss.get(i));
}
}
}


class Student {
private int num;
private String name;


public int getNum() {
return num;
}


public void setNum(int num) {
this.num = num;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


@Override
public String toString() {
return "Student [num=" + num + ", name=" + name + "]";
}


}


class StudentHelper {
List<Student> ls = new LinkedList<Student>();
List<Student> lsForCopy = new LinkedList<Student>();


public List<Student> getLsForCopy() {
return lsForCopy;
}


public void setLsForCopy(List<Student> lsForCopy) {
this.lsForCopy = lsForCopy;
}


public void addLsForCopyStudent(Student s) {
lsForCopy.add(s);
}


public StudentHelper() {
}


public StudentHelper(List<Student> ls) {
this.ls = ls;
}


public Student getStudentByNum(int v) {
for (int i = 0; i < ls.size(); i++) {
if (ls.get(i).getNum() == v) {
return ls.get(i);
}
}
return null;
}


public boolean checkStudentByNum(int v) {
return getStudentByNumFromLsForCoby(v) == null ? false : true;
}


public Student getStudentByNumFromLsForCoby(int v) {
for (int i = 0; i < lsForCopy.size(); i++) {
if (lsForCopy.get(i).getNum() == v) {
return lsForCopy.get(i);
}
}
return null;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值