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]