【问题标题】:String ordering with arraylist使用arraylist进行字符串排序
【发布时间】:2015-07-14 02:29:20
【问题描述】:

我有一个字符串列表,每行有 20 个字符串。第一行表示“任务”,第二行表示“任务之间的依赖关系”,第三行表示“每个任务的小时数”。我想根据 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++;
                        }


                    }

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

我想在添加新字符串后从头开始阅读列表。但是,如果已经添加了这样的字符串,我想跳到下一个。我怎么能那样做?最后,我希望有 20 个字符串分布在 2 个开发人员或列表中。

我的代码:

    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;
                        }*/
                   }
                }
         }

        }

【问题讨论】:

  • 那么,你的问题是什么?
  • 我的代码不起作用。最后我的输出是: >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
  • 使用对象!这将使您的生活更加简单。拥有TaskDeveloper 并相应地读取您的数据。 Java 是 OO,发挥它的威力。
  • 另外,Java 中的变量位于camelCase
  • @Boris 所说的使用对象让事情变得更简单。

标签: java arrays sorting


【解决方案1】:

您可以继续编写代码。你的第一个问题是内循环。你应该使用一个索引。 (这段代码仍然没有提供你写的结果。)但这更接近:

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

}

}

【讨论】:

  • 我正在运行代码,但它没有给出正确的输出。您的代码给出了以下输出: Cp1 任务:[t14, t7, t12, t5, t6, t13, t11, t2], hours: [81] ---Cp2 tasks: [t18, t15, t16, t10, t4, t19, t20, t1, t9, t8, t17, t3], hours: [86] ---- CP1的第一个任务应该是t4,因为它的依赖是0。
  • 给一个开发者添加任务后,我从字符串列表的开头重新开始。
  • 我在代码中发现了错误。将任务添加到一个 CP 后,如何设置 currentTask 和 currentDep 从位置 0 开始?换句话说,我想在找到并添加一个任务之后从头开始。
  • 所以在你找到它之后taskIndex = 0
  • 我尝试使用 'taskIndex = 0' 但不起作用。我不确定我是否在代码中放置了正确的位置。
【解决方案2】:

如果我是你,我会听从 cmets 的建议,因为如果你完成了它,阅读和维护代码会很糟糕,而且远离良好的实践。但是,如果您不想这样做,我会尽力提供一些帮助。这不是一个直接的解决方案,但如果我分享我的思考也许会有所帮助。 在我看来,你(你们俩)代码中的基本误解是两个主循环的同等水平。我的意思是,寻找 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()
  • 闲置:

    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
  • 空闲时: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()

在许多情况下,它会将输入划分为 2 个数组以获得 10 个字符串。开始如你所愿,但休息或多或少有所不同。我仍然认为这是向前迈出的一步。但是再一次,它写得不好,对不起。

【讨论】:

  • 你的评论对我一点帮助都没有。:(
  • @Jsi83 对不起。也许你应该重新考虑你的目标,稍微简化条件,然后当你成功时,你可以尝试实现其他的。
  • 我的代码几乎可以工作了。我的问题是逻辑。你能帮帮我吗?
  • @Jsi83 你说的几乎可以工作是什么意思?你之前做了什么?有什么效果?
  • 我的代码发现 0 和依赖。我的代码添加到两个不同的 Cps。但是在将新字符串添加到 Cp 后,我的代码不会开始。因此,并未添加所有 20 个字符串。我尝试设置taskIndex=0,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 2017-02-23
  • 2014-12-07
  • 2011-07-28
  • 2014-02-09
  • 2011-11-26
  • 2017-06-07
  • 1970-01-01
  • 2012-08-20
相关资源
最近更新 更多