We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a 2 dimensional object array and I want to set one of the array values objects equal to a previous one including all it's properties
I want to set:
for (int k=0; k<n; k++)
objectArray[j][k]=objectArray[j-1][k]
I tried the above but it seems to set all objectArray objects to have the same property values ie objectArray[j-2][k] now equals objectArray[j][k] as well?? Not sure why.
The following works.
for (int k=0; k<n; k++) {
objectArray[j][k].propertyA=objectArray[j-1][k].propertyA
objectArray[j][k].propertyB=objectArray[j-1][k].propertyB
objectArray[j][k].propertyC=objectArray[j-1][k].propertyC // etc
}
But do I really have to copy all properties to get this to work?
Thanks
Answers
If you intend to right-shift the whole array, you should save current index's element before replacing it w/ the 1 from its left. Otherwise, everything's gonna be the head index element! :-SS
Also if you wanna shorten the array by removing its head, you should apply subset() before start:
https://processing.org/reference/subset_.html
But 1 more important point is that you're only shifting the array's outer dimension.
Hence no need to touch its other inner dimensions. That's 1 less huge problem to deal w/: :bz
Alternatively, you can choose left-shifting. Which btW is much more common: =:)
Technically yes but there are better ways to do it.
Before I show you how, I need to explain the difference between an object reference and an object.
Consider the following code
In line 1 we have declared 2 object references of type Foo (f1 and f2) Java will initialise these to null. We have no objects yet - all objects are created with the new command so the one and only object is created in line 4.
In line 6 we have
f2 = f1
and many newbies think they are copying the object f1 and 'storing it' in f2. This is not true this statement simply copies the object reference.The output from this program is
Notice that changing a field in f0 (line 8) affects f2. That is because both f1 and f2 reference the same object. In many cases this is the desired outcome but if you want an independent copy (i.e. a second object) then you need to do something more.
There are different ways of getting an exact copy of an object and the method I demonstrate here is probably the simplest and uses a copy constructor.
A copy constructor is one that has a single parameter of the same type as the class.
Notice that in line 6 we invoke the copy constructor to create a new Foo object.
The out put from this program shows that we do in fact have 2 independent objects
So this
becomes this
You obviously have to rename the class Foo to your own.
HTH but it is a BIG topic area and it would be useful to see your class code to be sure I have provided enough info.
similar to quark:
PVector knows a copy method (method, not constructor) - it returns a type of it's class
then line 6 in the last sketch would be
f2 = f1.copy();
that'd be ok
the content of the method would return a object
;-)
true but the copy method PVector uses a constructor to create a new object.
You could make your own copy method but ultimately all objects have to be created using the
new
command with a constructor somewhere.AFAIK the only exception to using
new
would be retrieving objects previously serialized and creating them through Java reflection .but that would be a way for him to do it, right? use new in the method copy.
Well, after perusing my solution once more, realized that if I swap the loop type for right & left shifting cases, no temporary variables are needed at all, like those now & old! #-o
In short, for right-shifting, do backwards loop. That is, the opposite direction, from tail towards head indices.
And just invert the situation for left-shifting. A regular loop, from head towards tail indices! o=>
Yes that is just as good and in fact gives a clearer solution because all the work is hidden inside the copy method - good idea Chrisir
I have modified my last example to make use of a copy() method
Gonna leave the more simplified latest full version below for easier access: O:-)
Thanks so much for the help with this. Using the Copy constructor worked perfectly. A great technique to know about!!