Error to popular array with Double

Asked

Viewed 47 times

1

I need to put in a array of double the quantity field data coming from the database. By 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 a double array because I get the array as input in a function.

public static double fatorAmortecimentoExponencial(double... d) {
...
}
  • 1

    your array is instantiated and has this position?

  • Where and how the arrayinvertidoComUltimosCincoMeses?

  • Related question (not duplicate): https://answall.com/q/203324/132

2 answers

1

More details are missing in the question, but I believe that even if you have inserted this array, there should only be the same instance and no index to reference: In 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 set:

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

You should assign the size of the array only after your list.

  • This is an array, not a List.

  • Thank you for your attention and subtlety. (=

  • I did urge both

  • Add to post, please?

  • I added a comment at the end. As soon as you instantiate the list, it has 0 positions, and it is this size that you are assigning to the array. Assign values to the list first, then set the array size by the list size.

0


Let’s see that:

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

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

Whereas what you’re doing has to do with that other question of yours, I think the easiest thing for you would be to do like this:

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

// ...    

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

And then you change the method fatorAmortecimentoExponencial(double... d) to be a fatorAmortecimentoExponencial(List<Double> d).

Also, if you are using the method of my reply that iterates the list/array backwards, but you’re reversing it to give as input, it’s worth wondering if it’s not best to iterate the list/array in the direct order so you don’t have to pass it backwards.

If you want to pass the list as array itself, you already know that the size of the array 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();
}

That index [4 - i] leaves the resulting array in reverse order. To stay in direct order, you would simply use [i].

  • His reply helped me because I could insert my array later so it didn’t get zero.

Browser other questions tagged

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