In Java the size of the arrays are fixed, that is, once you create the X-size array you cannot add more elements to it. So when you create an array {{},{},{},{},{}}
you say they will all have size zero of elements.
This is exactly why this error is being generated. You are trying to access a space in the array that does not exist. The solution to solve this problem would be for you to declare how many positions the array will have.
private double[][] salarios = {{0,0},{0,0},{0,0},{0,0},{0,0}};
If you want to create an empty "array" to gradually add elements, use an ArrayList
. With it you can create as many elements as you want without having to declare their size before.
See this example below:
import java.util.ArrayList;
public class Main{
public static void main(String[] args){
ArrayList agenda = new ArrayList();
for (int i = 0; i < 10; i++){
agenda.add(i);
}
System.out.println(agenda);
}
}
Thank you so much for the @Jeanextreme002 explanation
– Renan Cavalcante