0

I tried shifting an array but I'm having issues.

The code to shift the array is as follows:

for(int i = (size - 1); i >= 0; i--)
{
    data2[i+1] = data2[i];
}

Array init (Copied from another array)

obj[] data = new obj[size];

obj[] data2 = new obj[size + 1];

for(int i = 1; i <= size; i++)
{
    data2[i] = data[i-1];
}

data2[0] = data[0];

For example, if size = 3, I only want to manipulate and use the data for data[1] -> data[3]. But, if the data for data[0] changes, the data for data[1] changes as well. What causes this?

ex:

Data 2[0]: 6----1----0----0

Data 2[1]: 6----1----0----0

Data 2[2]: 4----8----0----0

Data 2[3]: 9----5----0----0

data2[0].setElementTwo(3);

Data 2[0]: 6----3----0----0

Data 2[1]: 6----3----0----0

Data 2[2]: 4----8----0----0

Data 2[3]: 9----5----0----0

I'm copying the first array to the second because data[] is generated in another class which generates from 0->size, while I need 1->size+1 for this part of the program.

-edit for clarity-

full pseudo-code:

obj[] data = new obj[size];

obj[] data2 = new obj[size + 1];

for(int i = (size - 1); i >= 0; i--)
{
    data2[i+1] = data2[i];
}

for(int i = 1; i <= size; i++)
{
    data2[i] = data[i-1];
}

data2[0] = data[0];

// print data2 0->3

// change data2[0] value

// print data2 0->3, values would have changed for data[0] and data[1]
// but I only want to change values for data[0] and not data[1]
1
  • Unclear from the question what most of these variables have been initialised to and where in the code sequence are they manipulated. Also do specify clearly what you are trying to achieve. Commented Feb 4, 2017 at 7:32

2 Answers 2

2

First, let me clarify some concepts. obj is a reference type. Variables of reference types hold references to the actual objects. When you do aRefVariable = anotherRefVariable, you are not creating a new object. Instead, you are saying that aRefVariable should hold the same reference as anotherRefVariable. Basically, the two variables points to the same object after that line is executed.

The problem lies on this line:

data2[i+1] = data2[i];

You are saying that data2[i+1] should hold the same reference as data2[i].

To simplify the situation, let's suppose i is 0, so the line now becomes:

data2[1] = data2[0];

As I said, the second and first element of data2 points to the same object now. Accessing data2[0] is exactly the same as data2[1] in terms of the object you got.

Great, let's modify the object that the first item of data2:

data2[0].setElementTwo(3);

Now the object that data2[0] is referencing has been modified. When you try to access data2[1], you see the modification as well. Why? Because data2[0] and data2[1] refer to the same thing!

To fix this problem, you need to replace this line:

data2[i+1] = data2[i];

with something like this:

data2[i+1] = new obj(...); 
// you should create a new obj that has the same values as data2[i] using its constructor
Sign up to request clarification or add additional context in comments.

Comments

0

The new data2[] is referencing the old data[], so changes in the old will reflect in the new. If you don't want that use

 data[index] = new obj(data[i-1]) ;

This will create a new object, which will be basically a copy of the old object and mutually exclusive from one another.

Comments

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.