# (Noob question) why doesn't this line of code work?

**URL:** <https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284>\
**Category:** Coding Questions\
**Created:** [May 25, 2020, 3:08pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284 "2020-05-25T15:08:37Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [May 25, 2020, 3:08pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/1 "2020-05-25T15:08:37Z")

</div>

Consider the ArrayList(PVector) “p”.

Now let’s say I’m trying to set a vector in that list

```auto
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

```auto
list.set(index, object);

```

works well for any type

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 25, 2020, 3:29pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/2 "2020-05-25T15:29:32Z")

</div>

```auto

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);
}

```

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [May 25, 2020, 3:32pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/3 "2020-05-25T15:32:21Z")

</div>

aha, I knew it had something to do with set().

The real problem was my inability to find these resources on the Processing page 😆

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 25, 2020, 3:38pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/4 "2020-05-25T15:38:13Z")

</div>

> [@BennyHacker](#):
>
> was my inability to find these resources

> **[PVector / Reference](https://www.processing.org/reference/PVector.html)**
>
> A class to describe a two or three dimensional vector, specifically a Euclidean (also known as geometric) vector. A vector is an entity that has both magnitude and direction. The datatype, however, st…

> **[set() / Reference](https://www.processing.org/reference/PVector_set_.html)**
>
> Sets the x, y, and z component of the vector using two or three separate variables, the data from a PVector, or the values from a float array.

> **[PVector](https://www.processing.org/tutorials/pvector/)**
>
> An introduction to useing the PVector class in Processing.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 25, 2020, 3:43pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/5 "2020-05-25T15:43:49Z")

</div>

> [@BennyHacker](#):
>
> The real problem was my inability to find these resources on the Processing page

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:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 25, 2020, 5:32pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/6 "2020-05-25T17:32:18Z")

</div>

@BennyHacker

I did some further exploration into this…

Another tool is the “Code completion with Ctrl-space”:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0152ca8e7b04967e6d127206570f457123348d47.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/07415592da541997328a014d03e1a938bca3fbf3.png)

It gives me some insights into options for further exploration.

> **Original code (needs to be corrected):**
>
> ```auto
> 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**
>
> ```auto
> 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!

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 25, 2020, 6:34pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/7 "2020-05-25T18:34:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![uheinema](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@uheinema](https://discourse.processing.org/u/uheinema)\
**Post date:** [May 25, 2020, 7:07pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/8 "2020-05-25T19:07:09Z")

</div>

On the original question:

1. Something like anObject.aMethod()=… will never work, the result of a method call is no variable! ( anO.aMe().aPublic=… could)

2. 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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 25, 2020, 7:14pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/9 "2020-05-25T19:14:25Z")

</div>

Hello,

> [@Chrisir](#):
>
> there are reports it’s best to use PVector with pv.copy() when you copy. Because Pointer versus content

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](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().

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 25, 2020, 7:47pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/10 "2020-05-25T19:47:12Z")

</div>

> [@glv](#):
>
> 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.

```auto

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);
//

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 25, 2020, 9:00pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/11 "2020-05-25T21:00:43Z")

</div>

> [@Chrisir](#):
>
> Here is the proof

Thank you for correction.

Updating my notes.

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 25, 2020, 9:01pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/12 "2020-05-25T21:01:57Z")

</div>

Sorry, I wasn’t clear.

Chrisir 😉

---

<div class="post-metadata">

**Author:** ![uheinema](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@uheinema](https://discourse.processing.org/u/uheinema)\
**Post date:** [May 25, 2020, 10:07pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/13 "2020-05-25T22:07:51Z")

</div>

One more thimg, on the simple side:

You can actually do stuff like  
p.get(1).x=77.0;

, as x is public

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [May 26, 2020, 3:21pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/14 "2020-05-26T15:21:12Z")

</div>

That would help explain the “why”, thanks!

---

<div class="post-metadata">

**Author:** ![AngelRF](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AngelRF](https://discourse.processing.org/u/AngelRF)\
**Post date:** [May 27, 2020, 4:50pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/15 "2020-05-27T16:50:19Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 27, 2020, 5:46pm UTC](https://discourse.processing.org/t/noob-question-why-doesnt-this-line-of-code-work/21284/16 "2020-05-27T17:46:18Z")

</div>

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
