0
How do I after adding a string to a list (can be on list A or B), my code goes back and starts reading the beginning of the string list
, or the first string?
I have a list with 20 strings. But it’s giving an error because my code adds to string
and moves to the next position. It does not return and starts reading from the beginning. So it has strings that are not added.
How do I stop after adding a string, my code goes back and starts reading from the beginning (position 0 of the string list), ie from the first string?
Can someone give me an example? I’m racking my brain with this and I don’t go anywhere.
public int calcFitness(int indv) {
String linha1 = individualID.get(indv);
String[] taskQe = linha1.split(" ");
String linha2 = individualDep.get(indv);
String[] depQe = linha2.split(" ");
String linha3 = individualHour.get(indv);
String[] hourQe = linha3.split(" ");
String linha4 = individualEmpl.get(indv);
String[] emplQe = linha4.split(" ");
int fitness=0;
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))){
idle = Math.abs(Cp2TotalHours - Cp1TotalHours );
Cp1TotalHours=+idle;
if (Cp1TotalHours <= Cp2TotalHours){
Cp1.add(taskQe[i]);
Cp1Hour.add(Integer.parseInt(hourQe[i]));
Cp1Dep.add(depQe[i]);
Cp1TotalHours += (Integer.parseInt(hourQe[i]));
}
else{
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])));
Cp1TotalHours=+idle;
}*/
}
else if ((!Cp2.contains(currentDep)&& (Cp2TotalHours < Cp1TotalHours))){
idle = Math.abs(Cp2TotalHours - Cp1TotalHours );
Cp2TotalHours=+idle;
if (Cp2TotalHours < Cp1TotalHours){
Cp2.add(taskQe[i]);
Cp2Hour.add(Integer.parseInt(hourQe[i]));
Cp2Dep.add(depQe[i]);
Cp2TotalHours += (Integer.parseInt(hourQe[i]));
}
else{
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])));
Cp2TotalHours=+idle;
}*/
}
}
}
}
What is the language? It has how to put your code?
– Paulo Costa
@Paulocosta I am using java. I will post my code in the description of the question. I have 20 strings, but my code only adds 13.
– find83
When vc insert could not put the Indice = 0?
– Marconi
I did it @Marconi, but then it’s on a loop.
– find83
@Jsi83 is probably going into a loop when setting the index to zero because its logic must be in some trouble. It should always be falling in the situation where the index is set to zero. You need to ensure that at some point the index will not be set to zero and the loop will close. If you can, post what problem you are trying to solve. I don’t quite understand what you are doing. That way I can test the code and help you find the solution.
– Fagner Fonseca