使用arraylist进行字符串排序

时间:2015-05-04 16:04:44

标签: java arrays sorting

我有一个字符串列表,每行包含20个字符串。 fisrt线表示“任务”,第二个表示“任务之间的依赖”,第三个“每个任务的小时数”。我想按照3个标准订购。我还想将这个任务分配给2个开发人员(我称他们为Cp1和Cp2)。

我的标准:

  • 如果字符串X等于0,则用较少的小时将其添加到开发人员。

  • 如果已经执行(完成)任务依赖,请用较少的时间将其添加到开发人员。

  • 如果任务依赖性阻止了下一个任务,并且没有更多任务而没有依赖性,则计算非活动小时数,将其以较少的小时数添加到开发人员。

我的输入有3行:

  

t14 t18 t7 t12 t15 t5 t16 t10 t4 t19 t20 t6 t1 t13 t9 t8 t11 t17 t2 t3

     

t17 t2 t4 t16 t18 t4 t19 t4 0 t5 t1 t4 0 0 t2 t5 t14 t19 0 0

     

4 8 2 4 8 40 2 2 4 2 24 2 6 8 2 2 5 10 16 16

我的结果应该是:

  

Cp1:t4 t7 t10 t20 t6 t13 t2 t14 t18 t15 - 78小时

     

Cp2:t1 t5 t19 t16 t12 t8 t17 t9 t11 t3 - 89

我的代码提供了错误的输出:

  

Cp1:[t4,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2, t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4, t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1,t9,t2,t4,t1, t9,t2,t4,t1,t9,t2]小时:552

     

Cp2:[t1,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7, t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20,t7,t20]时间:550

为什么我的代码提供超过20个字符串?它应该在两个CP之间分配20个字符串。

为什么我的代码没有给出正确的顺序?

我的代码:

public int calcFitness(int indv) {

    String linha1 = individualID.get(indv);     
        idQe = linha1.split(" ");
    String linha2 = individualDep.get(indv);    
        depQe= linha2.split(" ");   
    String linha3 = individualHour.get(indv);
        hourQe = linha3.split(" ");
    String linha4 = individualEmpl.get(indv);
        emplQe = linha4.split(" "); 


        int count1 =0;
        int count2 =0;
        int idle =0;

        for (int x=0; x<idQe.length;x++){   
    //      int y = x + 1;
            for(int j=0;j<depQe.length;j++){    
                if(depQe[j].equals("0")){
                            if (count1<=count2){    
                                        Cp1.add(idQe[j]);
                                        Cp1Hour.add(hourQe[j]);
                                        Cp1Dep.add(depQe[j]);
                                        count1 += Integer.parseInt(hourQe[j]);
                                        }
                            else if(count2<count1){
                                Cp2.add(idQe[j]);
                                Cp2Hour.add(hourQe[j]);
                                Cp2Dep.add(depQe[j]);
                                count2 += Integer.parseInt(hourQe[j]);  
                                        }
                            }
                else if ((Cp1.contains(depQe[j])||(Cp2.contains(depQe[j])))){
                        if (count1<=count2){
                            if (!Cp1.contains(depQe[j])){
                                idle = Math.abs(count2-count1);
                                count1 += idle;
                                Cp1.add(idQe[j]);
                                Cp1Hour.add(hourQe[j]);
                                Cp1Dep.add(depQe[j]);
                                count1 += Integer.parseInt(hourQe[j]);
                                        }
                                            else {                                  
                                                    Cp1.add(idQe[j]);
                                                    Cp1Hour.add(hourQe[j]);
                                                    Cp1Dep.add(depQe[j]);
                                                    count1 += Integer.parseInt(hourQe[j]);
                                                    }
                                        }
                        else if(count2<count1){
                            if (!Cp2.contains(depQe[j])){
                                idle = Math.abs(count1-count2);
                                count2 += idle;
                                Cp2.add(idQe[j]);
                                Cp2Hour.add(hourQe[j]);
                                Cp2Dep.add(depQe[j]);
                                count2 += Integer.parseInt(hourQe[j]);  
                                    }
                                                else{
                                                    Cp2.add(idQe[j]);
                                                    Cp2Hour.add(hourQe[j]);
                                                    Cp2Dep.add(depQe[j]);
                                                    count2 += Integer.parseInt(hourQe[j]);
                                                        }
                                }               
                            }
                    j++;
                        }


                    }

我在代码中做了一些更改。我现在的问题是如何在添加新字符串后开始读取第一个字符串?

我想在添加新字符串后开始阅读列表。但是,如果已经添加了这样的字符串,我想跳转并获得下一个。我怎么能这样做?最后,我想在2个开发人员或列表中分发20个字符串。

我的代码:

    for (int taskIndex = 0; taskIndex < taskQe.length; taskIndex++) {
        String currentTask = taskQe[taskIndex];
        String currentDep = depQe[taskIndex];
        String currentHour = hourQe[taskIndex];
        System.out.println("task: [" + currentTask + "], dep: [" + currentDep + "], hours: [" + currentHour + "]");

        int hour = Integer.parseInt(currentHour);

        if (currentDep.equals("0")) {
        //  System.out.println ("axhou 0");
            if (Cp1TotalHours <= Cp2TotalHours) {
                Cp1.add(currentTask);
                Cp1Hour.add(hour);
                Cp1Dep.add(currentDep);
                Cp1TotalHours += hour;   
            } else {
                Cp2.add(currentTask);
                Cp2Hour.add(hour);
                Cp2Dep.add(currentDep);
                Cp2TotalHours += hour;
            }

        } else if (Cp1.contains(currentDep) || Cp2.contains(currentDep)) {
            //System.out.println ("achou dep" + currentDep );
            for (int i=0; i<=taskIndex; i++) {
            if (depQe[i].equals(currentDep)){
                System.out.println ("achou dep i and curr" + (taskQe[i]) + currentDep );
                if ((Cp1.contains(currentDep)&& (Cp1TotalHours <= Cp2TotalHours))){
                    Cp1.add(taskQe[i]);
                    Cp1Hour.add(Integer.parseInt(hourQe[i]));
                    Cp1Dep.add(depQe[i]);
                    Cp1TotalHours += (Integer.parseInt(hourQe[i]));
                }
                else if ((Cp2.contains(currentDep)&& (Cp2TotalHours < Cp1TotalHours))){
                    Cp2.add(taskQe[i]);
                    Cp2Hour.add(Integer.parseInt(hourQe[i]));
                    Cp2Dep.add(depQe[i]);
                    Cp2TotalHours += (Integer.parseInt(hourQe[i]));
                }
                else if ((!Cp1.contains(currentDep)&& (Cp1TotalHours <= Cp2TotalHours))){
                       Cp1.add(taskQe[i]);
                       Cp1Hour.add(Integer.parseInt(hourQe[i]));
                       Cp1Dep.add(depQe[i]);
                       Cp1TotalHours += (Integer.parseInt(hourQe[i]));  
                /*  for (int j=0; j<taskQe.length; j++) {   
                       if(depQe[i].equals(taskQe[j]))
                           idle = Math.abs(Integer.parseInt(hourQe[j]) - (Integer.parseInt(hourQe[i])));
                           Cp1TotalHours=+idle;
                    }*/
                }
                else if ((!Cp2.contains(currentDep)&& (Cp2TotalHours < Cp1TotalHours))){
                    Cp2.add(taskQe[i]);
                    Cp2Hour.add(Integer.parseInt(hourQe[i]));
                    Cp2Dep.add(depQe[i]);
                    Cp2TotalHours += (Integer.parseInt(hourQe[i])); 
                /*   for (int j=0; j<taskQe.length; j++) {  
                    if(depQe[i].equals(taskQe[j]))
                            idle = Math.abs(Integer.parseInt(hourQe[j]) - (Integer.parseInt(hourQe[i])));
                            Cp2TotalHours=+idle;
                        }*/
                   }
                }
         }

        }

2 个答案:

答案 0 :(得分:0)

You can continue with code. you first problem is the inner loop. you should use one index. (this code still does not provide the result you wrote.) but this is closer:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
public class Test1 {
   List<String> Cp1 = new ArrayList<String>();
   List<String> Cp1Dep = new ArrayList<String>();
   List<Integer> Cp1Hour = new ArrayList<Integer>();

   List<String> Cp2 = new ArrayList<String>();
   List<String> Cp2Dep = new ArrayList<String>();
   List<Integer> Cp2Hour = new ArrayList<Integer>();

   int Cp1TotalHours = 0;
   int Cp2TotalHours = 0;
   int idle = 0;

   public void calcFitness() {
     String linha1 = "t14 t18 t7 t12 t15 t5 t16 t10 t4 t19 t20 t6 t1 t13 t9 t8 t11 t17 t2 t3";
     String[] taskQe = linha1.split(" ");
     String linha2 = "t17 t2 t4 t16 t18 t4 t19 t4 0 t5 t1 t4 0 0 t2 t5 t14 t19 0 0";
     String[] depQe = linha2.split(" ");
     String linha3 = "4 8 2 4 8 40 2 2 4 2 24 2 6 8 2 2 5 10 16 16";
     String[] hourQe = linha3.split(" ");

     for (int taskIndex = 0; taskIndex < taskQe.length; taskIndex++) {
        String currentTask = taskQe[taskIndex];
        String currentDep = depQe[taskIndex];
        String currentHour = hourQe[taskIndex];
        System.out.println("task: [" + currentTask + "], dep: [" + currentDep + "], hours: [" + currentHour + "]");

        int hour = Integer.parseInt(currentHour);

        if (currentDep.equals("0")) {
            if (Cp1TotalHours <= Cp2TotalHours) {
                Cp1.add(currentTask);
                Cp1Hour.add(hour);
                Cp1Dep.add(currentDep);
                Cp1TotalHours += hour;
            } else {
                Cp2.add(currentTask);
                Cp2Hour.add(hour);
                Cp2Dep.add(currentDep);
                Cp2TotalHours += hour;
            }
        } else if (Cp1.contains(currentDep) || Cp2.contains(currentDep)) {
            if (Cp1TotalHours <= Cp2TotalHours) {
                if (!Cp1.contains(currentDep)) {
                    idle = Math.abs(Cp2TotalHours - Cp1TotalHours);
                    Cp1.add(currentTask);
                    Cp1Hour.add(hour);
                    Cp1Dep.add(currentDep);
                    Cp1TotalHours += hour;
                } else {
                    Cp1.add(currentTask);
                    Cp1Hour.add(hour);
                    Cp1Dep.add(currentDep);
                    Cp1TotalHours += hour;
                }
            } else {
                if (!Cp2.contains(currentDep)) {
                    idle = Math.abs(Cp1TotalHours - Cp2TotalHours);
                    Cp2.add(currentTask);
                    Cp2Hour.add(hour);
                    Cp2Dep.add(currentDep);
                    Cp2TotalHours += hour;
                } else {
                    Cp2.add(currentTask);
                    Cp2Hour.add(hour);
                    Cp2Dep.add(currentDep);
                    Cp2TotalHours += hour;
                }
            }
        } else {
            if (Cp1TotalHours <= Cp2TotalHours) {
                Cp1.add(currentTask);
                Cp1Hour.add(hour);
                Cp1Dep.add(currentDep);
                Cp1TotalHours += hour;
            } else {
                Cp2.add(currentTask);
                Cp2Hour.add(hour);
                Cp2Dep.add(currentDep);
                Cp2TotalHours += hour;
            }
        }
        System.out.println("Cp1 tasks: " + this.Cp1 + ", hours: [" + this.Cp1TotalHours + "]");
        System.out.println("Cp2 tasks: " + this.Cp2 + ", hours: [" + this.Cp2TotalHours + "]");
        System.out.println();
    }
}

public static void main(String[] args) {

    Test1 test1 = new Test1();

    test1.calcFitness();
    System.out.println();
    System.out.println();
    System.out.println("Cp1: " + test1.Cp1 + " hours: " + test1.Cp1TotalHours);
    System.out.println("Cp2: " + test1.Cp2 + " hours: " + test1.Cp2TotalHours);
    //Cp1: t4 t7 t10 t20 t6 t13 t2 t14 t18 t15 - 78 hours

    //Cp2: t1 t5 t19 t16 t12 t8 t17 t9 t11 t3 - 89

}

}

答案 1 :(得分:0)

如果我是你,我会遵循评论中的建议,因为如果你完成它,那么阅读和维护的代码就会很糟糕,而且远离良好的做法。但是,如果你不想,我会尝试帮助一点。这不是一个直接的解决方案,但如果我分享我的想法,它可能会有所帮助。 在我看来,你(你们两个)代码中的基本误解是两个主循环的相同级别。我的意思是,循环寻找0,并寻找任务的依赖,同时工作。

  • 在Joshi 83代码中,因为它,它可以找到大部分0,但它扰乱了所需的输入,并快速阻止依赖,
  • 在igreen代码中,如果不满足上述条件,他会添加一个添加任务的选项,因此它会添加第一个循环;

但如果我了解Joshi83的期望,他还想要别的东西。它不是线性添加任务,它更多:如果你找到0,跟踪一个依赖任务,如果没有,查找另一个0,但同时如果其他条件为真,则除去任务的其余部分。只是尝试绘制Jhoshi83预期输出顺序的草图,它看起来相当复杂。

括号中的数字是输入ArrayList的条件。这张图片很可悲,但它显示了它的复杂程度:P 我认为你应该至少有两个级别的主循环:

  • 第一个循环找到依赖链的开头,
  • 第二个主循环开始跟踪依赖关系,如果找到0,则启动另一个链

在一个过程中,您可以按照自己的意愿划分任务。另外我认为为ArrayList更改String []是个好主意,然后你可以删除已分配的任务,添加循环不会导致任务加倍。你可以这样做:

ArrayList<String> idQe = new ArrayList<String>(Arrays.asList(linha1.split(" ")));

然后使用idQe.get(j),而不是idQe [j]。

*编辑*

好的,我试过但我不知道,如何实施所有标准。但也许你会在这段代码中找到有用的东西。这是您更新的代码更新版本。我只想强调,这不是我认为应该做的!这就是我能用你的代码做的事情。我的解决方案不是太优雅,但仍然要检查出来:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
    List<String> Cp1 = new ArrayList<String>();
    List<String> Cp1Dep = new ArrayList<String>();
    List<String> Cp1Hour = new ArrayList<String>();

    List<String> Cp2 = new ArrayList<String>();
    List<String> Cp2Dep = new ArrayList<String>();
    List<String> Cp2Hour = new ArrayList<String>();
    boolean order = false;
    int Cp1TotalHours;
    int Cp2TotalHours;

    int idle = 0;

    public void calcFitness() {
        // everything in ArrayLists
        String linha1 = "t14 t18 t7 t12 t15 t5 t16 t10 t4 t19 t20 t6 t1 t13 t9 t8 t11 t17 t2 t3";
        ArrayList<String> taskQe = new ArrayList<String>(Arrays.asList(linha1.split(" ")));
        System.out.println(taskQe.toString());
        String linha2 = "t17 t2 t4 t16 t18 t4 t19 t4 0 t5 t1 t4 0 0 t2 t5 t14 t19 0 0";
        ArrayList<String> depQe = new ArrayList<String>(Arrays.asList(linha2.split(" ")));
        System.out.println(depQe.toString());
        String linha3 = "4 8 2 4 8 40 2 2 4 2 24 2 6 8 2 2 5 10 16 16";
        ArrayList<String> hourQe = new ArrayList<String>(Arrays.asList(linha3.split(" ")));

        //Loop for finding first task
        for(int i = 0; i < 20; i++){
            if (depQe.get(i).equals("0")) {
                if (Cp1Hour.size()<=Cp2Hour.size() ) {
                    Cp1.add(taskQe.get(i));
                    Cp1Hour.add(hourQe.get(i));
                    Cp1Dep.add(depQe.get(i));
                    System.out.println("CP1"+depQe.get(i));
                }else{
                    Cp2.add(taskQe.get(i));
                    Cp2Hour.add(hourQe.get(i));
                    Cp2Dep.add(depQe.get(i));
                    System.out.println("CP2"+depQe.get(i));
                    break;
                }
                System.out.println(depQe.get(i));
            }
        }

        //removing first tasks
        int num1 = taskQe.indexOf(Cp1.get(0));
        taskQe.remove(num1);
        depQe.remove(num1);
        hourQe.remove(num1);
        int num2 = taskQe.indexOf(Cp2.get(0));
        taskQe.remove(num2);
        depQe.remove(num2);
        hourQe.remove(num2);
        System.out.println(taskQe.toString());


        while(!taskQe.isEmpty()){
        for (int taskIndex = 0; taskIndex < taskQe.size(); taskIndex++) {
            String currentTask = taskQe.get(taskIndex);
            String currentDep = depQe.get(taskIndex);
            String currentHour = hourQe.get(taskIndex);
            System.out.println("task: [" + currentTask + "], dep: [" + currentDep + "], hours: [" + currentHour + "]");

            int hour = Integer.parseInt(currentHour);

            if (currentDep.equals("0")) {
                //if (Cp1TotalHours <= Cp2TotalHours) {       //   you can try
                if (Cp1Hour.size() <= Cp2Hour.size()){         //   both options
                    Cp1.add(currentTask);
                    Cp1Hour.add(currentHour);
                    Cp1Dep.add(currentDep);
                    Cp1TotalHours += hour;
                } else {
                    Cp2.add(currentTask);
                    Cp2Hour.add(currentHour);
                    Cp2Dep.add(currentDep);
                    Cp2TotalHours += hour;
                }
                //remove
                taskQe.remove(taskIndex);
                depQe.remove(taskIndex);
                hourQe.remove(taskIndex);
                taskIndex = 0;                           // try with and without
            } else if (Cp1.contains(currentDep) || Cp2.contains(currentDep)) {
                //if (Cp1TotalHours <= Cp2TotalHours) {       //   you can try
                if (Cp1Hour.size() <= Cp2Hour.size()){         //   both options
                    if (!Cp1.contains(currentDep)) {
                        //idle = Math.abs(Cp2TotalHours - Cp1TotalHours);
                        Cp1.add(currentTask);
                        Cp1Hour.add(currentHour);
                        Cp1Dep.add(currentDep);
                        Cp1TotalHours += idle + hour;
                    } else {
                        Cp1.add(currentTask);
                        Cp1Hour.add(currentHour);
                        Cp1Dep.add(currentDep);
                        Cp1TotalHours += hour;
                    }
                } else {
                    if (!Cp2.contains(currentDep)) {
                        //idle = Math.abs(Cp1TotalHours - Cp2TotalHours);
                        Cp2.add(currentTask);
                        Cp2Hour.add(currentHour);
                        Cp2Dep.add(currentDep);
                        Cp2TotalHours += idle + hour;
                    } else {
                        Cp2.add(currentTask);
                        Cp2Hour.add(currentHour);
                        Cp2Dep.add(currentDep);
                        Cp2TotalHours += hour;
                    }
                }
                //remove
                taskQe.remove(taskIndex);
                depQe.remove(taskIndex);
                hourQe.remove(taskIndex);

                //taskIndex = 0;
            }

        }

        }
        System.out.println(Cp1 +" - "+ Cp1TotalHours +"hours");
        System.out.println(Cp2 +" - "+ Cp2TotalHours +"hours");
    }

    public static void main(String[] args) {

        Test test1 = new Test();

        test1.calcFitness();

        //Cp1: t4 t7 t10 t20 t6 t13 t2 t14 t18 t15 - 78 hours

        //Cp2: t1 t5 t19 t16 t12 t8 t17 t9 t11 t3 - 89

    }
}

我需要承认,我错了,嵌套循环结构并不那么重要。有几行你可以尝试不同的输出,如下:

  • f(Cp1Hour.size()&lt; = Cp2Hour.size()){2x taskIndex = 0;没空闲:        Cp1:[t4,t7,t10,t16,t20,t13,t17,t18,t9,t14] - 62小时Cp2:[t1,t5,t19,t12,t6,t8,t2,t15,t3,t11] - 95小时
  • 闲置:

    Cp1:[t4,t7,t10,t16,t20,t13,t17,t18,t9,t14] - 120小时Cp2 [t1,t5, t19,t12,t6,t8,t2,t15,t3,t11] - 145小时。

  • if(Cp1TotalHours&lt; = Cp2TotalHours){2x taskIndex = 0;没空闲: Cp1:[t4,t7,t10,t19,t16,t12,t20,t6,t13,t2,t9,t3] - 80小时 Cp2:[t1,t5,t8,t17,t18,t15,t14,t11] - 77小时
  • 空闲:Cp1:[t4,t7,t10,t19,t12,t6,t13,t8,t2,t15,t3] - 120小时Cp2:[t1,t5,t16,t20,t17,t18,t9,t14,t11] - 115小时
  • (Cp1Hour.size()&lt; = Cp2Hour.size()){no taskIndex = 0 in(if 0)loop; 没空闲: Cp1:[t4,t7,t10,t13,t8,t3,t2,t15,t14,t12] - 62小时 Cp2:[t1,t5,t20,t19,t17,t16,t18,t9,t6,t11] - 95hours

在许多情况下,它将2个数组的输入除以10个字符串。一开始就是你想要的,但休息或多或少是不同的。我仍然认为这是向前迈进的一步。但是再一次,写得很糟糕,对不起。