Add element to List (Arraylist)

0

I am having trouble adding values that are in a List named Processes to the Aptos list. Always show only the last process, and duplicate. Even so, remove all previous processes.

Code below:

public void verificaTodosProcessosAptos(){
   int i;
   for(i=0;i<FilaProcessos.size();i++){
       if(FilaProcessos.get(i).getTempoCriacao()<TSP){
           saiDeProcessoParaAptos();
           i--;
       }
   }
}

public void saiDeProcessoParaAptos(){
       //apto=FilaAptos.get(indiceAptos);
       processo=FilaProcessos.get(0);
       apto.setPid(processo.getPid());
       apto.setNome(processo.getNome());
       apto.setTempoCriacao(processo.getTempoCriacao());
       apto.setTempoExecucao(processo.getTempoExecucao());
       if (TSF == 0) 
            apto.setEsperaFila(0);
       else
            apto.setEsperaFila(TSP - apto.getTempoCriacao());
       apto.setSaidaFila(apto.getTempoCriacao() + apto.getEsperaFila());
       TSF=apto.getSaidaFila();
       armazenarFilaApto(apto);
       indiceAptos++;
       excluirFilaProcessos(processo);
       indiceProcesso--;
   }

public void armazenarFilaApto(Aptos a){
        FilaAptos.add(a);
}

I do not know if they could understand, I clarify any doubts about my question.

    
asked by anonymous 26.09.2014 / 14:23

4 answers

1

I rewrote your code, simplifying a few steps and worked Ok, resulting in the expected output and not only displaying the last element of FilaProcessos , check out:

import java.util.ArrayList;
import java.util.List;

public class Main {

    static List<String> filaProcessos = new ArrayList<String>();
    static List<String> filaAptos = new ArrayList<String>();

    public static void main(String[] args) {
        filaProcessos.add("um");
        filaProcessos.add("dois");
        filaProcessos.add("tres");

        verificaTodosProcessosAptos();

        for (String s : filaAptos) { 
            System.out.println(s);
        }
    }

    public static void verificaTodosProcessosAptos(){
        int i;
        for(i=0;i<filaProcessos.size();i++){
            saiDeProcessoParaAptos();
            i--;
        }
    }

    public static void saiDeProcessoParaAptos(){
        String processo = filaProcessos.get(0);
        String apto = processo;

        armazenarFilaApto(apto);
        //indiceAptos++;
        filaProcessos.remove(processo);
        //indiceProcesso--;
    }

    public static void armazenarFilaApto(String a){
        filaAptos.add(a);
    }

}
    
26.09.2014 / 16:57
0

Look at my friend, I had a problem similar to this and the fault was in the lack of instance, a new object at each iteration, for example, finalized a cycle of for I have to give new in class objects .

So I realize you're using global variables, right? Then one checks where you are instantiating the apt object or another one you are using.

    
26.09.2014 / 14:40
0

If apt and processes are two lists that have the same type of values you can use:

aptos.addAll(processos);
    
26.09.2014 / 17:02
0

Good morning. Your methods are a bit confusing, start by simplifying them. I'll comment the code to try to explain.

public void verificaTodosProcessosAptos(){
   //Este metodo faz a mesma coisa que você quer fazer, mas sem indice (você não precisa dele)
   for(Processo proc : FilaProcessos){
       if(proc.getTempoCriacao()<TSP){
           saiDeProcessoParaAptos(prc);
       }
   }
}

//Neste método, você não precisa pegar o FilaProcessos.get(0), basta passar o processo por parâmetro, acredito que seja isto que você quer.

public void saiDeProcessoParaAptos(Processo proc){

       apto.setPid(proc.getPid());
       apto.setNome(proc.getNome());
       apto.setTempoCriacao(proc.getTempoCriacao());
       apto.setTempoExecucao(proc.getTempoExecucao());
       if (TSF == 0) 
            apto.setEsperaFila(0);
       else
            apto.setEsperaFila(TSP - apto.getTempoCriacao());
       apto.setSaidaFila(apto.getTempoCriacao() + apto.getEsperaFila());
       TSF=apto.getSaidaFila();
       armazenarFilaApto(apto);
       indiceAptos++;
       excluirFilaProcessos(proc);
       indiceProcesso--;
   }

From here on you should test your code, if you have difficulty finding the error with only logs, try debugging your project!

If you find it difficult to understand what I wrote, I will ask you to respond as much as possible.

Hugs

    
26.09.2014 / 14:47