Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用arraylist进行字符串排序_Java_Arrays_Sorting - Fatal编程技术网

Java 使用arraylist进行字符串排序

Java 使用arraylist进行字符串排序,java,arrays,sorting,Java,Arrays,Sorting,我有一个字符串列表,每行有20个字符串。第一行表示“任务”,第二行表示“任务之间的依赖关系”,第三行表示“每个任务的小时数”。我想根据3个标准来订购。我还想将此任务分配给2个开发人员(我称他们为Cp1和Cp2) 我的标准: 如果字符串X等于0,则将其添加到开发人员中,时间更短 如果已经执行(完成)了任务依赖性,则将其添加到开发人员中,时间更短 如果任务解除依赖阻止了下一个任务,并且没有更多没有解除依赖的任务,则计算非活动小时数,将其添加到开发人员中,时间更短 我的输入有3行: t14 t18

我有一个字符串列表,每行有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 t2 t5 t14 t19 0

4 8 2 4 8 40 2 2 4 2 24 2 6 8 2 5 10 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、t4、t2、t2、t2、t2、以及t1[Cp1:[Cp1:[Cp1:[Cp1:[Cp1:[Cp1:[t4、t2、t2、t2、t2、t2、t2、t2、t2、t2、t2、t2、t2、t9、t9、t1、t2、t9、t9、t9、t9、t9、T、t9、t9、t2、t4、t9、t2、t4、T44、T44、T44、T、T、T44、T44、T、T、T、T47、T、T、t9、t9、t4、t4、T、t9、t9、t9、t4、T8、T到到到到到到到到到到到到到到到到到到到到那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个那个时间:552小时

Cp2:[t1、t7、t20、t7、t20、t7、t20、t7、t20、t7、t20、t7、t20、t7、t20、t7、t20、t7、t20、t20、t7、t20、t7、t20、t7、t20、t7、t20、t7、t20、t20、t20、t7、t20、t7、t20、t20、t20、t7、t20、t20、t7、t20、t20、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++;
                        }


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

        }
public int calcFitness(int indv){
字符串linha1=individualID.get(indv);
idQe=linha1.拆分(“”);
字符串linha2=individualDep.get(indv);
depQe=linha2.拆分(“”);
字符串linha3=individualHour.get(indv);
hourQe=linha3.拆分(“”);
字符串linha4=individualemp.get(indv);
emplQe=linha4.拆分(“”);
int count1=0;
int count2=0;
int idle=0;

对于(int x=0;x您可以继续使用代码。您的第一个问题是内部循环。您应该使用一个索引。(此代码仍然没有提供您编写的结果。)但这更接近:

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

}
import java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.util.*;
公共类Test1{
List Cp1=new ArrayList();
List Cp1Dep=new ArrayList();
List Cp1Hour=new ArrayList();
List Cp2=new ArrayList();
List Cp2Dep=new ArrayList();
List Cp2Hour=new ArrayList();
int CP1总小时=0;
int Cp2TotalHours=0;
int idle=0;
公共无效计算能力(){
字符串linha1=“t14 t18 t7 t12 t15 t5 t16 t10 t4 t19 t20 t6 t1 t13 t9 t8 t11 t17 t2 t3”;
字符串[]taskQe=linha1.split(“”);
字符串linha2=“t17 t2 t4 t16 t18 t4 t19 t4 0 t5 t1 t4 0 t2 t4 t19 0”;
字符串[]depQe=linha2.split(“”);
字符串linha3=“4 8 2 4 8 40 2 2 4 2 24 2 6 8 2 5 10 16”;
字符串[]hourQe=linha3.split(“”);
对于(int taskIndex=0;taskIndex如果(Cp1TotalHours如果我是你,我会按照评论中的建议来做,因为如果你完成了,阅读和维护代码会很糟糕,而且远离良好的实践。但是如果你不想,我会尝试帮助你一点。这不是一个直接的解决方案,但如果我分享我的想法,可能会有所帮助。
在我看来,你们(你们两个)代码中的基本误解是两个主循环的级别相同。我的意思是,寻找0的循环和寻找任务依赖性的循环同时工作

  • 在Joshi 83代码中,正因为如此,它可以找到大多数0,但它干扰了所需的输入,并快速阻塞了依赖关系
  • 在igreen代码中,他添加了一个选项,如果不满足上述条件,则添加任务,因此它将其添加到第一个循环中
但如果我理解Joshi83的期望值,他还想要别的东西。这不是线性添加任务,更多的是:如果找到0,跟踪一个依赖的任务,如果没有更多,再寻找另一个0,但同时如果其他条件为真,则划分任务的其余部分。只要试着画一个Jhoshi83期望输出顺序的草图,它看起来相当复杂。

括号中的数字是输入ArrayList的标准。这张图很可怜,但它显示了它有多么复杂:p 我认为您应该向我们提供至少两级主回路:

  • 第一个循环找到依赖链的开始
  • 第二个主循环开始跟踪依赖项,如果找到0,则开始另一个链
在一个过程中,您可以根据需要在它们之间划分任务。另外,我认为最好更改ArrayList的字符串[],然后您可以删除分配的任务,添加循环不会导致任务加倍。您可以这样做:

ArrayList<String> idQe = new ArrayList<String>(Arrays.asList(linha1.split(" ")));
ArrayList idQe=新的ArrayList(Arrays.asList(linha1.split(“”));
然后使用idQe.get(j),而不是idQe[j]

*编辑*

好的,我试过了,但我不知道如何实现所有标准。但也许你会在这段代码中发现一些有用的东西。这是你更新代码的更改版本。我只想强调一下,这不是我认为应该怎么做的!这是w
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

    }
}