coconut
coconut

Reputation: 1101

Writing to arrays in Java

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.

Upvotes: 1

Views: 324

Answers (6)

Matthew Lewis
Matthew Lewis

Reputation: 483

I suspect the error is being caused by the line:

if ((recursoA > 1) && (recursoA <= recursos)){

What value is recursos being set to? If it is above numberDependencias then an out of bounds error would make sense. Additionally, the open brace at the end of that line doesn't seem to be being closed. This will certainly cause an error!

Upvotes: 3

Alex M
Alex M

Reputation: 136

Your problem is right here numberDependencias > 0, because you initialze an array with number of elements starting at 0. When you start to fill at numberDependencias, you're try to put in an element out of the arrays size. Try numberDependencias-1 > 0 and use array[numberDependencias] instead of recursoA, excepting you want to initialize your array with recursoA.

Instead of an array I would prefer java.util.HashMap where you can easily put key-value pairs into, besides that you don't need to worry about initializing your size or out of bounds errors.

Upvotes: 0

daniel gratzer
daniel gratzer

Reputation: 53881

Well I believe what you're saying that you are creating an array that's the size of the number of these "pairs" you have. However, this will give you out of range errors if you have a number larger then the number of pairs, you will get errors. If this is unclear, say you have 2 pairs, (9, 3) and (3, 4), and you try to save them in your array as dependencias[3] = 9 and dependencias[4] = 3. Your going to get errors as dependencias is not large enough for those values to be used as indexes.

To fix this use a Map, which is made like this,

Map<Integer, Integer> dependencias = new HashMap<Integer, Integer>();

then you can add things to your map, like so:

dependencias.put(someInteger1, someInteger2);

Finally you can call back that pair using:

dependencias.get(someInteger1); //this will return someInteger2

Upvotes: 0

Mike Baranczak
Mike Baranczak

Reputation: 8374

A Java array has fixed bounds. If you declare it like this:

a = new int[3];

then you can only assign values to a[0], a[1] and a[2]. What you want here is not an array but a Map.

Map<Integer, Integer> dependencias = new HashMap<Integer, Integer>();
dependencias.put(4000, 2000);

dependencias.get(4000) // returns 2000

Upvotes: 3

Aaron
Aaron

Reputation: 57748

The integer you're reading-in (recursoA) has to be greater than 1 and less than "recursos". The problem, is that "recursos" is probably greater than "numberDependencias"...which is the max size of your array.

Either alter this line to make sure that recursoA is always less then numberDependencias.

if ((recursoA > 1) && (recursoA < numberDependencias)){

Or, define your array size to be "recursos".

dependencias = new int[recursos];

Upvotes: 3

pcalcao
pcalcao

Reputation: 15990

What values are you trying to introduce for recursoA?

I think you should check if this value is between 0 and numberDependencias-1. Keep in mind that arrays begin at 0, so if you create an array with 4 positions, you can assign values to array[0] ... array[3].

Upvotes: 0

Related Questions