转置后再排列整理

【问题】

CONTEXT: I'm writing a program that fills a school timetable of 7 hours and 5 days based on the available hours to teach from the professors. The data for the professors its taken from a text file.

Main class:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Main{
    static ArrayList<profesor>profesors = new ArrayList<profesor>();
    static String timetable[][] = new String[7][5];



    public static void main(String[]args) throws Exception{
        timetable[0][0] = "lua"; timetable[0][1] = "maa"; timetable[0][2] = "mia"; timetable[0][3] = "jua"; timetable[0][4] = "via";
        timetable[1][0] = "lub"; timetable[1][1] = "mab"; timetable[1][2] = "mib"; timetable[1][3] = "jub"; timetable[1][4] = "vib";
        timetable[2][0] = "luc"; timetable[2][1] = "mac"; timetable[2][2] = "mic"; timetable[2][3] = "juc"; timetable[2][4] = "vic";
        timetable[3][0] = "lud"; timetable[3][1] = "mad"; timetable[3][2] = "mid"; timetable[3][3] = "jud"; timetable[3][4] = "vid";
        timetable[4][0] = "lue"; timetable[4][1] = "mae"; timetable[4][2] = "mie"; timetable[4][3] = "jue"; timetable[4][4] = "vie";
        timetable[5][0] = "luf"; timetable[5][1] = "maf"; timetable[5][2] = "mif"; timetable[5][3] = "juf"; timetable[5][4] = "vif";
        timetable[6][0] = "lug"; timetable[6][1] = "mag"; timetable[6][2] = "mig"; timetable[6][3] = "jug"; timetable[6][4] = "vig";
        readList();
        fillTimetable();    
        displayTimetable();


    }
    private static void readList() throws Exception {
        FileReader file = new FileReader("list.txt");
        BufferedReader reader=new BufferedReader(file);
        String line =reader.readLine();
        String lineSplit[]= new String[27];
        while(line != null){
            lineSplit = line.split("\\s+");
            profesors.add(new profesor(lineSplit[0], lineSplit[1], new String[]{ lineSplit[2], lineSplit[3], lineSplit[4], lineSplit[5], lineSplit[6], lineSplit[7],lineSplit[8], lineSplit[9], lineSplit[10], lineSplit[11], lineSplit[12], lineSplit[13], lineSplit[14], lineSplit[15], lineSplit[16], lineSplit[17], lineSplit[18], lineSplit[19], lineSplit[20], lineSplit[21], lineSplit[22], lineSplit[23], lineSplit[24], lineSplit[25], lineSplit[26]}));
            line = reader.readLine();

        }

    }
    private static void fillTimetable() {
        String[] materias = new String[13];
        materias[0] = "Matematica";
        materias[1] = "Apesca";
        materias[2] = "NavegacionI";
        materias[3] = "TecPesc";
        materias[4] = "MeteorologiaI";
        materias[5] = "ComunicacionesI";
        materias[6] = "IngTecI";
        materias[7] = "Supervivencia";
        materias[8] = "ManiobraI";
        materias[9] = "MaquinasI";
        materias[10] = "EquiposElectronicos";
        materias[11] = "PrimerosAux";
        materias[12] = "CargaEstiva";
        int counterMatematica = 1;
        int counterApesca = 1;
        int counterNavegacionI = 1;
        int counterTecPesc = 1;
        int counterMeteorologiaI = 1;
        int counterComunicacionesI = 1;
        int counterIngTecI = 1;
        int counterSupervivencia = 1;
        int counterManiobraI = 1;
        int counterMaquinasI = 1;
        int counterEquiposElectronicos = 1;
        int counterPrimerosAux = 1;
        int counterCargaEstiva = 1;


        for(int a = 0, p = 0, i=0, k=0;i<34;p++, i++){
            if(p == 6){//23
                k++;
                p = 0;

            }
            if (k==5){break;}
            if (a==23){break;}
            //i periods k days
            for(int j=0;j<profesors.size();j++){
                String[] ah = profesors.get(j).getAvalideHours();
                String pname = profesors.get(j).getName();
                String psubject = profesors.get(j).getSubject();
                for(int y = 0; y<24;y++){
                    if(ah[y].equals(timetable[p][k]) ){
                        if(psubject.equals(materias[0])){
                            if(counterMatematica<=2){
                                timetable[p][k] = psubject + " " + pname;
                                counterMatematica++;
                                break;
                            }}
                        if(psubject.equals(materias[1])){
                            if(counterApesca<=3){
                                timetable[p][k] = psubject + " " + pname;
                                counterApesca++;
                                break;
                            }}
                        if(psubject.equals(materias[2])){
                            if(counterNavegacionI<=4){
                                timetable[p][k] = psubject + " " + pname;
                                counterNavegacionI++;
                                break;
                            }}
                        if(psubject.equals(materias[3])){
                            if(counterTecPesc<=2){
                                timetable[p][k] = psubject + " " + pname;
                                counterTecPesc++;
                                break;
                            }}
                        if(psubject.equals(materias[4])){
                            if(counterMeteorologiaI<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterMeteorologiaI++;
                                break;
                            }}
                        if(psubject.equals(materias[5])){
                            if(counterComunicacionesI<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterComunicacionesI++;
                                break;
                            }}
                        if(psubject.equals(materias[6])){
                            if(counterIngTecI<=2){
                                timetable[p][k] = psubject + " " + pname;
                                counterIngTecI++;
                                break;
                            }}
                        if(psubject.equals(materias[7])){
                            if(counterSupervivencia<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterSupervivencia++;
                                break;
                            }}
                        if(psubject.equals(materias[8])){
                            if(counterManiobraI<=3){
                                timetable[p][k] = psubject + " " + pname;
                                counterManiobraI++;
                                break;
                            }}
                        if(psubject.equals(materias[9])){
                            if(counterMaquinasI<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterMaquinasI++;
                                break;
                            }}
                        if(psubject.equals(materias[10])){
                            if(counterEquiposElectronicos<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterEquiposElectronicos++;
                                break;
                            }}
                        if(psubject.equals(materias[11])){
                            if(counterPrimerosAux<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterPrimerosAux++;
                                break;
                            }}
                        if(psubject.equals(materias[12])){
                            if(counterCargaEstiva<=1){
                                timetable[p][k] = psubject + " " + pname;
                                counterCargaEstiva++;
                                break;
                            }}

                        }

                    }
                }
            }

        }



    private static void displayTimetable() {
        // TODO Auto-generated method stub

    }

}

profesor class:

import java.util.Arrays;
public class profesor {
    private String name;
    private String subject;
    private String[] avalidehours=new String[25];
    public profesor(String string, String string2, String[] strings) {

        name = string;
        subject = string2;
        avalidehours = strings;
    }
    public String toString(){
        return name + subject + Arrays.toString(avalidehours);
    }
    public String getName(){
        return name;
    }
    public String getSubject(){
        return subject;
    }
    public String[] getAvalideHours(){
        return avalidehours;
    }

}

This is the text file:

Petitti Matematica  mif mig vif vig ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Canales Apesca  luc lud mac mad mic mid juc jud ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Lucero  NavegacionI lub luc lud lue mab mac mad mae mib mic mid mie jub juc jud jue vib vic vid vie ññ  ññ  ññ  ññ  ññ
Bergamaschi TecPesc lua luf maa maf mia mif jua juf via vif ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Mazza   MeteorologiaI   mab mac jub juc ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Puebla  ComunicacionesI lua lub luc lud maa mab mac mad mia mib mic mid jua jub juc jud via vib bic bid ññ  ññ  ññ  ññ  ññ
Chiatti IngTecI mib mic mid mie jub juc jud jue ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Jimenez Supervivencia   maa mab jua jub ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Rubino  ManiobraI   mab mac mad mae maf mib mic mid mie mif ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Velasco MaquinasI   vib vic vid vie vif ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ  ññ
Luceroo EquiposElectronicos lub luc lud lue mab mac mad mae mib mic mid mie jub juc jud jue vib vic vid vie ññ ññ ññ ññ ññ
Atenas PrimerosAux vib vic vid vie ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ
Fuster CargaEstiva lue luf lug mae maf mag mie mif mig jue juf jug vie vif vig  ññ ññ ññ ññ ññ ññ ññ ññ ññ ññ

And this is the result timetable I get :

monday tuesday wednesday thursday friday
7 null null null null null
6 TecPesc ManiobraI Matematica CargaEstiva Matematica
5 NavegacionI ManiobraI EquiposElectronicos null null
4 Apesca NavegacionI ManiobraI null null
3 Apesca Apesca IngTecI MeteorologiaI PrimerosAux
2 NavegacionI NavegacionI IngTecI Supervivencia MaquinasI
1 TecPesc ComunicacionesI null null null

Problems:

So i have two problems with the ouput
1 I get 2 hours of the same subject in the same day but one its for example in hour 1 and the other at hour 6.
2 I get empty spaces in the middle of the day and that cannot happen.
so the question its how can i solve this problems?
beacouse i have no idea of what to do to solve them
Thanks in advance and sorry for the bad english.

【回答】

只要解决同一时段多名教授的问题,就可制作出正确的时间表,思路如下:先根据文本文件创造两个字段的二维表A2professor是教授的名字,codeArray是该教授对应的时间代码列表。

再构造5*6小时的时间码表A3(根据你的代码)。

之后循环A3,从A2中选出codeArray包含当前时间代码的那些professor

最后造时间表(字段是周1到周5),将选出的professor组合依次填进去,或者将professor组合拼成逗号分隔的字符串再填。

上述算法涉及结构化运算、多层循环、集合查找,用JAVA实现时代码复杂不易理解。如无特殊要求可用SPL解决,代码简单易懂:


A

1

=file("d:\\source.txt").import(;" ")

2

=A1.new(#2:professor,~.array().to(3,27).select(~!="ññ"):codeArray)

3

=["lua","maa","mia","jua","via","lub","mab","mib","jub","vib",   "luc","mac","mic","juc","vic",   "lud","mad","mid","jud","vid",   "lue","mae","mie","jue","vie",   "luf","maf","mif","juf","vif",   "lug","mag","mig","jug","vig"]

4

=A3.(A2.select(codeArray.pos(A3.~)).(professor))

5

=create(monday,tuesday,wednesday,thursday,friday).record(A4.(~.concat@c()))

 

A1:读取文本文件source.txt中的内容,分隔符为空格。

undefined

A2:对A1进行处理,创造字段为professorcodeArray的二维表。professor是教授的名字对应第二列数据。codeArray是该教授对应的时间代码列表,对应第3-27列中不等于ññ的数据。

undefined

A3:构造5*6小时的时间码表

A4:循环A3,从A2中选出codeArray包含当前时间代码的那些professor

undefined

A5:构造时间表(字段是周1到周5),将professor组合拼成逗号分隔的字符串再依次填进去。

undefined

上述代码很容易和JAVA集成,参考Java 如何调用 SPL 脚本