# Printing all PVector coordinates stored in an ArrayList

**URL:** <https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485>\
**Category:** Beginners\
**Created:** [November 17, 2020, 1:18pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485 "2020-11-17T13:18:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![papaboo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/papaboo/32/11236_2.png) [@papaboo](https://discourse.processing.org/u/papaboo)\
**Post date:** [November 17, 2020, 1:18pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/1 "2020-11-17T13:18:44Z")

</div>

I’m trying to get the whole set of x, y coordinates that are stored in an array.

```auto
ArrayList<Points> recorded = new ArrayList(); 

void store (float x, float y) {
  
  int ArrayListSize = recorded.size()-1;
 
  if (ArrayListSize>=0) {
    PVector p1 = recorded.get(ArrayListSize).p1; 
 
    if (p1.x!=x || p1.y!=y)
      recorded.add(new Points(x, y));
     println(x);
     println(y + ",");
  } 
  else {
    recorded.add(new Points(x, y));
  }
}

class Points {
 
  PVector p1;
 
  Points(float x_, float y_) {
 
    p1=new PVector(x_, y_);
  }
  
}

```

These lines:

```
 println(x);
 println(y + ",");

```

just print while drawing. And a code like this:

```auto
      if (recorded.size() == 100) {
      for (int i = 0; i < 100; i++) {
        println(recorded.get(i));
      }
      }

```

Just returns the index numbers like **sketch$Points@39a69ce2**

What is the way to get the list of x,y values all at once after reaching a certain number of them? (in my example it’s the 100th)

Like this:

```auto
141.60295
528.31396,
148.41893
538.1505,

```

etc

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [November 17, 2020, 1:42pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/2 "2020-11-17T13:42:55Z")

</div>

Hi @papaboo,

My suggestion would be to simplify things 🙂  
So the PVector stores already x and y, so you don’t need a new class

> **[PVector \\ Language (API) \\ Processing 3+](https://processing.org/reference/PVector.html)**

Find my example below. Everytime you click on the canvas, the mouse coordinates are added to the record. Once you have clicked 10 times (in this case), all previous records will be printed 🙂

```auto
ArrayList<PVector> recorded = new ArrayList(); 

void setup() {
  size(100, 100);
}

void draw() {  
}

void mousePressed() {
  recorded.add(new PVector(mouseX, mouseY)); //Add new coordinates to ArrayList
  int idxLastRecord = recorded.size()-1; //Get the last added position in the array
  println("Add to record: " + recorded.get(idxLastRecord).x + ", " + recorded.get(idxLastRecord).y); //Print the coordinates

  if (recorded.size() == 10) { //If the size is ten, print all
    println("I am the one! PRINTING THEM ALL! ");
    for (int i = 0; i < recorded.size(); i++) {
      println("X = " + recorded.get(i).x + ", Y = " + recorded.get(i).y);
    }
    println("--------------------------");
  }
}

```

Hope it helps!  
Best regards

---

<div class="post-metadata">

**Author:** ![papaboo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/papaboo/32/11236_2.png) [@papaboo](https://discourse.processing.org/u/papaboo)\
**Post date:** [November 17, 2020, 1:59pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/3 "2020-11-17T13:59:06Z")

</div>

Thanks @MiguelSanches but I still don’t understand why a statement like this doesn’t work for my code :

```auto
  if (recorded.size() == 100) {
      for (int i = 0; i < 100; i++) {
        println("X = " + recorded.get(i).x + ", Y = " + recorded.get(i).y);
      }
      }

```

Why does it give an error when I add **.x** or **.y** after **get(i)**?  
That was my question, basically.

---

<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:** [November 17, 2020, 2:18pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/4 "2020-11-17T14:18:08Z")

</div>

> [@papaboo](#):
>
> Why does it give an error when I add **.x** or **.y** after **get(i)**?

Are you running those line in your code you posted above? With `ArrayList<Points> recorded ` ?

Then here

```auto
recorded.get(i).x 

```

you forgot p1 (which is located in the class Points), no?

```auto
recorded.get(i).p1.x 

```

because ArrayList is of type Points

Chrisir

---

<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:** [November 17, 2020, 2:29pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/5 "2020-11-17T14:29:43Z")

</div>

another version

```auto

ArrayList<PVector> recorded = new ArrayList(); 

void setup() {
  size(1110, 400);
  background(0);
}

void draw() {
  background(0); 
  for (PVector pv : recorded) // short form of for-loop
    ellipse(pv.x, pv.y, 7, 7);
}

void mousePressed() {
  //Add new coordinates to ArrayList
  recorded.add(new PVector(mouseX, mouseY)); 

  //Get the last added position in the array
  PVector pvTest = recorded.get(recorded.size()-1);
  println("Add to record: "  
    +pvToString(pvTest)); //Print the coordinates

  if (recorded.size() == 10) { //If the size is ten, print all
    println("I am the one! PRINTING THEM ALL! ");
    for (PVector pv : recorded)
      println(pvToString(pv));

    println("--------------------------");
  }
}

// function to get String from PVector
String pvToString (PVector pv) {
  return
    "X = " + pv.x 
    + ", Y = " + pv.y;
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 17, 2020, 5:05pm UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/6 "2020-11-17T17:05:56Z")

</div>

> [@papaboo](#):
>
> Just returns the index numbers like **sketch$Points@39a69ce2**

> [@Print class variables](https://discourse.processing.org/t/print-class-variables/8142/2):
>
> A class needs to [@Override](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Override.html) the [toString()](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#toString()) method, so it can return a String w/ the current value of its fields. coffee Here’s a link w/ an example for it: link[Forum.Processing.org/two/discussion/25986/how-can-i-replace-pvector-tostring#Item\_7](http://Forum.Processing.org/two/discussion/25986/how-can-i-replace-pvector-tostring#Item_7)

Other examples w/ **toString()**: 🤓

> [@How to reference an object in another function to its class?](https://discourse.processing.org/t/how-to-reference-an-object-in-another-function-to-its-class/17964/2):
>
> 1 way to do it: angel Person squid; void setup() { frameRate(1); squid = new Person(); } void draw() { squid.increaseAge(); // alt.: ++squid.age; print(squid); } class Person { int age; Person increaseAge() { ++age; return this; } String toString() { return "Age: " + age + TAB; } }

> [@Objects in objects](https://discourse.processing.org/t/objects-in-objects/22486/2):
>
> In general an object shouldn’t need to be aware of any other object which “owns” it. But if you feel you need such feature you can create an “owner” field for it: /\*\* \* AppleTree (v1.0.0) \* GoToLoop (2020/Jul/08) \* https://Discourse.Processing.org/t/objects-in-objects/22486/2 \*/ import java.util.List; Tree tree; void setup() { tree = new Tree().bearFruit().bearFruit(); println(tree); println(tree.apples); exit(); } class Apple { Tree owner; Apple() { } Apple(final Tr…

> [@Accessing functions from Java Generics](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391/9):
>
> Even If we had 2 or more classes w/ the exactly same code, Java would still “see” them as different from each: void setup() { MyVec vec1 = new MyVec(); MyVecTwin vec2 = new MyVecTwin(); println(MyVec.class.isInstance(vec1)); // true println(MyVec.class.isInstance(vec2)); // false exit(); } class MyVec { float x = random(-100, 100), y = random(-100, 100), z; @Override String toString() { return "[" + x + ", " + y + ", " + z + "]"; } } class MyVecTwin { // exactly same…

> [@Parent function not using overwritten array from child](https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796/3):
>
> You’re not overriding but overshadowing the parent’s class field named array inside each of its child classes! Overriding is for non-static methods only. Other types of class members become overshadowed instead. So when subclassing take extra care not to accidentally re-declare inherited fields and static methods. If you need so instead assign a value w/o re-declaring them. // https://Discourse.Processing.org/t/ // parent-function-not-using-overwritten-array-from-child/22796/3 // GoToLoop …

---

<div class="post-metadata">

**Author:** ![papaboo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/papaboo/32/11236_2.png) [@papaboo](https://discourse.processing.org/u/papaboo)\
**Post date:** [November 24, 2020, 3:27am UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/7 "2020-11-24T03:27:04Z")

</div>

Thanks @Chrisir, I’ve got another question related to this. To animate an object (a red circle in this particular example) along the points drawn; I’m using the code below in **draw()** but the red circle ends up at the end of the path (at the last vertex in the array).

I thought **for loop** in Processing was iterating each time a new frame is created in draw() function, relative to the frame rate. But this one happens all at once. I searched the forum and the answers suggested using lerp(), which wouldn’t work in my case because the time between two point is set to 1 frame- irrespective of the distance between them. Should I use millis or pushMatrix, popMatrix? Keep in mind this is a 120fps power-intensive sketch, so I’m really confused.

Here’s the loop:

```auto
if (readyToAnimate && mouseX > 1000) {
    for (int i = 0; i < recorded.size() - 1; i++) {
      background(48);
      fill(255, 0,0);
      ellipse((recorded.get(i).p1.x), recorded.get(i).p1.y, 30, 30);
    
    }
}

```

---

<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:** [November 24, 2020, 6:55am UTC](https://discourse.processing.org/t/printing-all-pvector-coordinates-stored-in-an-arraylist/25485/8 "2020-11-24T06:55:50Z")

</div>

**Explanation**

draw() updates the screen only one time at its end.

Therefore you cannot see any animation done with a for loop.

Get rid of the for loop and then use the fact that draw is looping in itself.

So make `i` a global variable and increase it with `i++;`

When i is `>= size()` of the list say `i = 0;` to restart

**The lerp() command**

Later you can use lerp() too. It would help to calculate and show the minor points between each or two points in the chain. So the animation gets more smooth. You would need i2 to do this and reset i2 every time you reach a point (which you received by i).

Normally we use a fixed number of steps (amt parameter in the reference for `lerp()`) between two points. To have always the same speed of the animation, we could calculate the number of steps based on the distance of the two current points.

Chrisir
