Consider the ArrayList(PVector) “p”.
Now let’s say I’m trying to set a vector in that list
p.get(i)=p.get(i+1).copy();
When I write this it says “left hand of assignment must be variable”.
How do I get around this?
Edit: furthermore to the solution, I found that
list.set(index, object);
works well for any type
1 Like
ArrayList<PVector> l1 = new ArrayList();
size(700, 700);
l1.add(new PVector (12, 7));
l1.add(new PVector (22, 22));
l1.add(new PVector (222, 222));
l1.get(1).set (l1.get(2).copy()); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for (PVector pv : l1 ) {
rect(pv.x, pv.y, 3, 6);
}
3 Likes
aha, I knew it had something to do with set().
The real problem was my inability to find these resources on the Processing page
1 Like
glv
May 25, 2020, 3:43pm
5
One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate them.
This is a very short list:
Resources < Click here to expand !
I encourage you to review the resources available here:
:)
4 Likes
glv
May 25, 2020, 5:32pm
6
@BennyHacker
I did some further exploration into this…
Another tool is the “Code completion with Ctrl-space”:
It gives me some insights into options for further exploration.
Original code (needs to be corrected):
ArrayList<PVector> p = new ArrayList();
PVector v1, v2, v3;
v1 = new PVector(0, 1);
v2 = new PVector(2, 3);
v3 = new PVector(4, 5);
p.add(v1);
p.add(v2);
p.add(v3);
printArray(p);
p.set(1, p.get(2));
printArray(p);
p.set(1, v2);
printArray(p);
// ArrayList
// https://processing.org/reference/ArrayList.html
// Also links to:
// https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
// https://processing.org/reference/ArrayList.html
// PVector
// https://processing.org/reference/PVector.html
// https://processing.org/reference/PVector.html
Corrected code
To be updated!
See comments in topic.
I have a lot of snippets of code like this that I write which I add to my resources.
And sometimes they need to be corrected!
:)
2 Likes
Oh, the collection would interest me
Also, there are reports it’s best to use PVector with pv.copy() when you copy. Because Pointer versus content
On the original question:
Something like anObject.aMethod()=… will never work, the result of a method call is no variable! ( anO.aMe().aPublic=… could)
Beware of the overloaded Object-setters… Hard to find bugs if you put in a long instead of int, never to find it again. But you don’t need to understand that.
Best regards
1 Like
glv
May 25, 2020, 7:14pm
9
Hello,
I was using the get(index) method of ArrayList in my example.
I did find this in the source for using get() and copy() methods with PVectors:
https://github.com/processing/processing/blob/master/core/src/processing/core/PVector.java#L359
They appear to be the same and get() (depracated) just calls copy().
:)
You got me wrong there.
I was trying to say your code is wrong, since you forgot to use .copy() when trying to copy a PVector: p.set(1,p.get(2));
.
You want p.set(1,p.get(2).copy);
Here is the proof (with wrong line):
We receive 19,19 in 2 slots of the arrayList, not in one slot.
ArrayList<PVector> p = new ArrayList();
PVector v1, v2, v3;
v1 = new PVector(0, 1);
v2 = new PVector(2, 3);
v3 = new PVector(4, 5);
p.add(v1);
p.add(v2);
p.add(v3);
printArray(p);
//-----------------------------------------------
p.set(1, p.get(2));
printArray(p);
p.get(2).set(19, 19);
printArray(p);
//
1 Like
glv
May 25, 2020, 9:00pm
11
Chrisir:
Here is the proof
Thank you for correction.
Updating my notes.
:)
1 Like
One more thimg, on the simple side:
You can actually do stuff like
p.get(1).x=77.0;
, as x is public
2 Likes
That would help explain the “why”, thanks!
I think that get() gets a reference to the object, so you can’t set a reference (i’m not sure, also i come from C++ so i call this a pointer)
That’s right:
when trying to copy a PVector: p.set(1,p.get(2));
it only gets you the pointer / address. So later the PVector in slot 1 changes when you change slot 2. Not what you want.
You want p.set(1,p.get(2).copy);
to get the content (value) into 2 and make it independent of 1.
I tried to explain briefly above.
Chrisir