0

In my code below, I am storing current array in ArrayList of Arrays and whatever change I make on current array are reflected back in ArrayList at the same time? how is this happening ? But in else block, when I intialize current array with another array, it doesn't change in ArrayList.

List<int[]> res=new ArrayList<>();
    int[] current=intervals[0];
    res.add(current);
    for(int[] interval:intervals){
        int first=current[0];
        int second=current[1];
        int third=interval[0];
        int fourth=interval[1];
        if(second>=third){
            current[1]=Math.max(second,fourth);
        }
        else{
            current=interval;
            res.add(current);
        }
    }
1
  • I get that I am making changes on same object in the if block and that's why the current array is changing in the list. But in else block, when I update the current arraay which is stored in list with another array then why is the current array in list not changing? You know when i update the current array and intialise it with another array, that is also change in current array and should reflect back in list. Commented Apr 16, 2020 at 15:48

2 Answers 2

1

You have to take in account that by assigning intervals[0] to int[] current, you are not actually creating a new object. So your field current points to same object as intervals[0]. So when you call res.add(current), you are actually adding the array stored in intervals[0] to the List and any changes made in the current field will also be done in the array added to the List (because it is the same object). And as far as the code tells, you are not doing any changes on the array in else block, that's maybe why no changes are visible :P. If you do not want the array changes to be reflected in the list, before adding the array to the list, create a new array object and initialize it for example this way:

int[] current = new int[intervals[0].length]
for(int i = 0; i < intervals[0].length; ++i)
   current[i] = intervals[0][i]

For your second question, if you have your array initialized like this:

int[][] intervals = new int[size][];
for(int i = 0; i < size; ++i)
   intervals[i] = new int[size2];

that means that you created a new array (new object) inside each cell of the array. Now. This code:

int[] current=intervals[0];

Makes your variable current to point on the same object as intervals[0] does. So when you call res.add(current); you add the object current is pointing to to the list. So any changes made on current, or intervals[0] will also be reflected in the object stored in the list because it is the same object. But when you then assign another object to the current, when you call current = interval; you are just saying, that current now points to same object as interval does. That does not change the attributes of the original object current was pointing to (intervals[0]), current will be just pointing to another object.

Sign up to request clarification or add additional context in comments.

6 Comments

intervals seems to be an array of arrays of int, since author could execute for(int[] interval:intervals)
yes, still he's not creating new object by that assignment
Right, I mean your line current[i] = intervals[i]woudln't compile
I get that I am making changes on same object in the if block and that's why the current array is changing in the list. But in else block, when I update the current arraay which is stored in list with another array then why is the current array in list not changing? You know when i update the current array and intialise it with another array, that is also change in current array and should reflect back in list.
@AmanDhaka I updated my answer, check it out and ask if I was not clear enough :P
|
0

A complete example of your program could help to answer to second part of your question. The central point is the pointer reference, and the resign of current has an impact to the second part of your question.

The array is a pointer to the memory where the "integers are located". At first when you add the intervals[0] into the res list, actually you are inserting a pointer address to the same memory. When you switch/change the values to the current they're reflected to res, because they points to the same memory. You can think at current[1]=Math.max(second,fourth); as: Get the address memory of the array and change its value at position 1. At this point the previous value in res in updated, because the address is the same. You can think it like an alias, they act like streets that goes to the same target.

3 Comments

I get that I am making changes on same object in the if block and that's why the current array is changing in the list. But in else block, when I update the current arraay which is stored in list with another array then why is the current array in list not changing? You know when i update the current array and intialise it with another array, that is also change in current array and should reflect back in list.
In the else block, when you update current with interval, you're switching the current reference to another "address". This means you're writining the address value of interval into current. The res value which you put into list before is not updated because you don't change its value, but you put a new one inside. Res is going to have antoer reference plus the previous one. A debug view with your IDE could help you more to understand what's happening and this article could help you more javax0.wordpress.com/2016/01/06/pointers-in-java
Hey Thanks. Now I get it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.