Error with popular array with Double

1

I need to put in a array of double the data of the quantity field that comes from the database. When doing this with the following code:

static List<Historico> listaComCincoUltimosMeses = new ArrayList<Historico>
 ();
static double[] arrayinvertidoComUltimosCincoMeses = new double[listaComCincoUltimosMeses.size()];


  int k = 0;
  for (Historico hist : listaComCincoUltimosMeses) {                
     System.out.println(hist.getMesesHistoricos() == null ? "Erro" : 
        hist.getMesesHistoricos());

     System.out.println(hist.getQuantidade());
     arrayinvertidoComUltimosCincoMeses[k] = hist.getQuantidade();
     System.out.println(arrayinvertidoComUltimosCincoMeses.length);             
     System.out.println("Array"+ arrayinvertidoComUltimosCincoMeses[k]);
     k++;
}

I get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

Does anyone know why?

I want to put in an array of double because I get the array as input into a function.

public static double fatorAmortecimentoExponencial(double... d) {
...
}
    
asked by anonymous 11.05.2017 / 21:00

2 answers

0

Let's look at this:

static List<Historico> listaComCincoUltimosMeses = new ArrayList<Historico>
 ();
static double[] arrayinvertidoComUltimosCincoMeses = new double[listaComCincoUltimosMeses.size()];

First a% void is created, then ArrayList will be zero. Soon you will create an empty array with zero elements. Attempting to access any position in an array with zero elements causes a listaComCincoUltimosMeses.size() .

Since what you're doing is about that other question of yours , I think the easiest for you would be doing so:

static List<Historico> listaComCincoUltimosMeses = new ArrayList<>();

// ...    

List<Double> ultimosCincoMeses = listaComCincoUltimosMeses.stream().map(Historico::getQuantidade).collect(Collectors.toList());
Collections.reverse(ultimosCincoMeses);

And so, you change the method ArrayIndexOutOfBoundsException to be a fatorAmortecimentoExponencial(double... d) .

Also, if you are using the method of this my response that iterates the list / array back to front, but you are reversing it to give as input, it is worth thinking if the best is not to make the list / array be iterated in direct order so you do not have to pass it inverted.

If you want to pass the list as an array, you already know that the array size is 5:

static List<Historico> listaComCincoUltimosMeses = new ArrayList<>();

// ...

double[] array = new double[5];
for (int i = 0; i < 5; i++) {
    array[4 - i] = listaComCincoUltimosMeses.get(i).getQuantidade();
}

This fatorAmortecimentoExponencial(List<Double> d) index leaves the resulting array in reverse order. To be in direct order, you would simply use [4 - i] .

    
11.05.2017 / 22:12
1

More details are missing in the question, but I believe that even if you have invoked this array, there should only be the same instance and no index to reference: In the case of list, you can use:

arrayinvertidoComUltimosCincoMeses.add(k, hist.getQuantidade())

For the array, make sure that in your instance the number of positions is defined:

double[] arrayinvertidoComUltimosCincoMeses = new double[liataComCincoUltimosMeses.size()];

You should only assign the size of the array after yourComputerLastMessels list is populated, otherwise it will have 0 positions, so the return will fail.

    
11.05.2017 / 21:38