Add element in List (Arraylist)

Asked

Viewed 1,762 times

0

I’m having trouble adding values that are in a list called Processes to the Aptos list. Always shows only the last process, and in duplicate form. Still removes 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 you can understand, anything I clarify the doubts about my question.

  • 2

    Can you put it in code? I can’t see the image.

  • how I format the code here?

  • Give 4 spaces at the beginning of the line, he already does the 'rest'

  • in the fear "saiDeProcersParaAptos()", you are always picking up Filaprocessos.get(0). I do not advise you to try to manipulate the content of a for. It can also get in the way!

  • You’d also find it interesting that you put the code in, not a photo!

  • If it is too large, four spaces in each line of code, if it is small, thing of a method or reserved word, use Shift+ before and after the highlighted snippet.

Show 1 more comment

4 answers

1

I rewrote your code, simplifying a few steps and it worked OK, resulting in the expected output and not just presenting 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);
    }

}

0

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

As far as I can tell, you’re using global variables, right? So check where you’re instantiating the fit object or another one you’re using.

  • I think this was it Maique. At least it got better when I created the new instance. Thanks

0

Good morning. Your methods are a little confused, start by simplifying them. I will comment on 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 now on you should go testing your code, if you have difficulties finding the error only with logs, try Debug your project!

If you find it difficult to understand what I have written you will ask that as far as possible I answer.

Hugs

0

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

aptos.addAll(processos);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.