Storing values in a for loop?

9.8k views Asked by At

I'm new to Java, and I'm having problems with a math test program I'm creating (no worries; the issue isn't a mathematical calculation). I'm putting an array of doubles that all equal zero through a for loop that'll assign a random number to them. They retain the random number, but then those numbers are reset to zero once it exits the for loop. I thought once I stored the changed number in a loop, it was permanently changed, but is this not the case? Here's the code segment:

        int a = 0;
        int b = 0;
        int c = 0;
        double x = 0;
        Random num = new Random();
        int[] abc = {a, b, c};
        for(int i = 0; i <= (abc.length-1); i++){
            int nums = num.nextInt(10);
            abc[i] = nums;
            System.out.println(abc[i]);
        }
        System.out.println(a + " " + b + " " + c);

I put a system.out in and out of the for loop to see what prints. I'm not quite certain why they all become zeros again, or how I'm supposed to properly revalue the elements. Any help is appreciated. Thank you for your time.

4

There are 4 answers

0
Ankit Chaudhary On

It is working properly,But you are checking wrong variables.You are not changing values of a,b & c variables you are changing values of abc. If you check abc it will give you correct values:

int a = 0;
int b = 0;
int c = 0;
double x = 0;
Random num = new Random();
int[] abc = {a, b, c};
for(int i = 0; i <= (abc.length-1); i++){
    int nums = num.nextInt(10);
    abc[i] = nums;
    System.out.println(abc[i]);
}
System.out.println(abc[0] + " " + abc[1] + " " + abc[2]);
2
Tirma On

Your abc array is created with a copy of a, b and c values. It does not really contains a reference to a, b and c.

When you modify abc[i] you are modifiyng the array, bau not the values of a, b or C.

If you want to print the values changed, you have to print the content of the array abc.

0
A.S On

when you are passing variables a,b and c to the array they are called by value which means that a copy of the variable is created and passes into the array so the original variables a, b and c will not be effected.

in java primitives are called by value.

unlike objects in java are called by reference which means that the original object will be effected. here you just need to add this:

a=abc[0];
b=abc[1];
c=abc[2]; 
0
AudioBubble On

Java is always call by value. In your example:

int a = 0;
int b = 0;
int c = 0;
...
int[] abc = {a, b, c};// only values are copied 
for(int i = 0; i <= (abc.length-1); i++){
    ...
    abc[i] = nums; //updating index elements of array, not variables a, b or c
    ...
}
System.out.println(a + " " + b + " " + c); // all zeros as initial

The code works as expected, there is no value reset after for-loop.

If you are still looking for updating value, here is the work around(it is call by value):

import java.util.Random;

class Ideone {
    public static void main (String[] args) {
        Data a = new Data(0),
            b = new Data(0),
            c = new Data(0);
        Data[] abc = {a, b, c}; //copied reference as value
        Random rand = new Random();
        for(Data d : abc) {
            d.i = rand.nextInt(10); //updating value at reference
            System.out.printf("d = %s\n", d);
        }
        System.out.printf("a = %s, b = %s, c = %s\n", a,b,c);//updated
    }
}

class Data {
    int i;
    public Data(int i) {
        this.i = i;
    }
    @Override
    public String toString() {
        return Integer.toString(i);
    }
}