Page 1 of 1

How this code works?

Posted: Mon Feb 12, 2018 6:30 pm
by maty.tsoraro@gmail.com
hi
can some one explain me excatly how this code works?
i know it defines a an arr of 4
but what each line does ? :geek:
http://www.beta.browxy.com/#tabs-USER_1 ... Value.java
http://www.beta.browxy.com/#tabs-USER_1 ... Value.java

public class Value{
private int _i;
public Value(int i){
_i = i;
}
public int getI(){
return _i;
}
public void setI (int i){
_i = i;
}

}


public class TestValue{
public static void main(String[] args){
Value[] arr = new Value[4];
for(int i = 0; i<arr.length; i++){
arr = new Value(i);
}
Value t = arr[0];
arr[0] = arr[3];
arr[3] = t;
t.setI(arr[0].getI());
for(int i = 0; i<arr.length; i++)
System.out.print(arr.getI()+ " ");

}
}

Re: How this code works?

Posted: Mon Feb 12, 2018 7:15 pm
by dbremmen@gmail.com
Hi!

To allow anybody to see your program you can paste the code (surrounded with the code tag, use the button </> in the editor so it's pretty formatted) and also:

1) Right Click on your project, select Edit Project Properties and uncheck the private checkbox (this is to allow anybody to see your program) and click "Save"
2) Right click on your project and click "Get Url", this will give you the URL to share your program
3) Paste the URL in the post

If somebody then use the URL, they will get a clone of your project and can help you faster

Re: How this code works?

Posted: Mon Feb 12, 2018 7:21 pm
by maty.tsoraro@gmail.com

Re: How this code works?

Posted: Mon Feb 12, 2018 7:46 pm
by dbremmen@gmail.com
Hi, here is the code pasted using the code tags

Code: Select all

public class TestValue{
    public static void main(String[] args) {
        Value[] arr  = new Value[4];
        for(int i = 0; i<arr.length; i++) {
          arr[i] = new Value(i);
        }
        Value t = arr[0];
        arr[0] = arr[3];
        arr[3] = t;
        t.setI(arr[0].getI());
        for(int i = 0; i<arr.length; i++) {
          System.out.print(arr[i].getI()+ "  ");    
        }
    }
}

public class Value{
    private int _i;
    public Value(int i){
        _i = i;
    }
    public int getI(){
        return _i;
    }
    public void setI (int i){
       _i = i;
    }
    
}

What this code does is the following:

a) Value[] arr = new Value[4]; initializes an empty array objects of type Value of size 4

b) The first for iterates from numbers from 0 to 4 and associate to the index of the arrays the following objects:
arr[0] = Value(0)
arr[1] = Value(1)
arr[2] = Value(2)
arr[3] = Value(3)
So at this point you will have in your array: 0 1 2 3

c) The statement: Value t = arr[0]; will assign to the variable t the value of arr[0] that is Value(0)

d) The statement: arr[0] = arr[3]; will assign to arr[0] the value of arr[3] that is: Value(3)
So at this point you will have in your array: 3 1 2 3

e) The statement: arr[3] = t; will assign to arr[3] the value of arr[0]
So, at this point you will have in your array 3 1 2 3

f) The statement: t.setI(arr[0].getI()); will assing to t the value of arr[0]
So, at this point you will have in your array 3 1 2 3

g) The last for iterates the array and print each value

Re: How this code works?

Posted: Tue Feb 13, 2018 4:21 am
by maty.tsoraro@gmail.com
hi,
i think i got it..
can you please explain more regarding f) & g)
the get and set in this method changes nothing ?

Re: How this code works?

Posted: Tue Feb 13, 2018 3:12 pm
by dbremmen@gmail.com
Hi!

This stamentent: t.setI(arr[0].getI()); do the following
To see what's happening is easy to decompose it
I will write an equivalent piece of code

Value valueObject = arr[0]; // valueObject has the object that arr[0] reference to

int value = valueObject.getI(); // value contains the result of calling the method getI() that returns the value stored in the private _i variable of the object, that in this case is 3

t.setI(value); // this statement sets the value of the object t to 3

Now, the object t was stored in the array at position 3. So the last statement change the private variable t to "3" that is the same value that has before

In this case you need to see the difference between three things
a) The array store pointers to objects of type Value
b) The set/get methods change values inside objects of type Value
c) This operation for example change the pointer of the array: arr[3] = t; because it set the pointer of position 3 of the array to the object t

Re: How this code works?

Posted: Thu Feb 15, 2018 4:19 am
by maty.tsoraro@gmail.com
thank you very muבי
will need to review this explanitation few more times,but i am starting to understand :)