I have an array of integers and I want to save pairs of integers using the index of the array as one of the indexes and the value as the other integer.
For example, if I wanted to save the pair: 2, 4. I could do: dependencias[2] = 4 (or dependencias[4] = 2.
It should be fairly simple, right? But I can't do it! I keep getting an out of bounds error. This is my code:
int recursoA, recursoB;
dependencias = new int[numberDependencias];
/* I tried setting them all to 0, to see if that was the problem.
* It didn't do anything, as I expected. */
for (int i = 0; i < numberDependencias; i++){
dependencias[i] = 0;
}
int recursoA, recursoB,cont = 0;
while (numberDependencias > 0){
System.out.println("Introduce el primer recurso");
recursoA = Integer.parseInt(br.readLine());
if ((recursoA > 1) && (recursoA <= recursos)){
System.out.println("Introduce el segundo recurso");
recursoB = Integer.parseInt(br.readLine());
dependencias[recursoA] = recursoB; // This is the problem, apparently.
numberDependencias--;
}
Sorry the variables are in Spanish, but hopefully you'll see what I'm talking about. I can't translate it right now.