# LinkedList vs ArrayList

**URL:** <https://discourse.processing.org/t/linkedlist-vs-arraylist/19946>\
**Category:** Coding Questions\
**Created:** [April 20, 2020, 12:17am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946 "2020-04-20T00:17:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 12:17am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/1 "2020-04-20T00:17:15Z")

</div>

Code below is perfectly working, but unfortunately, OpenProcessing does not accept the LinkedList import. The other imports seem to be accepted.  
I would like to use ArrayList instead, but I get the “does not match” error. What could be the solution?

```auto
import java.awt.Point;
import java.util.Queue;
import java.util.LinkedList;

void setup() {
  int x = 10, y = 20;
  Queue<Point> queue = new LinkedList<Point>();
  queue.add(new Point(x, y));
  println(queue);
}

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 5:44am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/2 "2020-04-20T05:44:09Z")

</div>

It seems that LinkedList and ArrayList are not interchangeable.  
So I am trying to code a custom Queue Class with ArrayList itself.  
I managed to code it with Integer, but I would like to use PVector to get rid of all imports.  
However, I have problems to add a PVector to the ArrayList.  
It gives the error:

> The method add() in the type sketch\_200420a.PVectorQueue is not applicable for the arguments (int, int, int)

What should be changed?

```auto
void setup() {
  PVectorQueue pvq = new PVectorQueue();
  for (int i = 0; i <= 10; i++) {
    pvq.add(i,i,i);
  }  
}  

public class PVectorQueue {
  int front; // front points to front element in the queue (if any)
  int x, y, z;
  ArrayList<PVector> myArray = new ArrayList<PVector>();

  PVectorQueue() {
    front = 0;
  }

  public PVector add() {
    myArray.add(new PVector(x,y,z));
  }

  public PVector remove() {
    PVector res = myArray.get(front);
    myArray.remove(0);    
    return res;
  } 

  public PVector peek() {
    if (isEmpty()) {
      println("UnderFlow\nProgram Terminated");
      exit();
    } 
    return (PVector) myArray.get(0);
  }

  public int size() {
    return myArray.size();
  }

  public Boolean isEmpty() {
    return (size() == 0);
  }
}

```

---

<div class="post-metadata">

**Author:** ![poboyd](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/poboyd/32/8908_2.png) [@poboyd](https://discourse.processing.org/u/poboyd)\
**Post date:** [April 20, 2020, 8:05am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/3 "2020-04-20T08:05:55Z")

</div>

have you tried using float instead of int for…  
int x, y, z;

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 10:48am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/4 "2020-04-20T10:48:28Z")

</div>

> [@noel](#):
>
> pvq.add(i,i,i);

Change to

```auto
pvq.add(new PVector(i,i,i));

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 10:53am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/5 "2020-04-20T10:53:34Z")

</div>

Hi. Thanks for answering.  
But it gives the error:

> The method add() in the type sketch\_200420a.PVectorQueue is not applicable for the arguments (PVector)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 10:55am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/6 "2020-04-20T10:55:08Z")

</div>

> [@noel](#):
>
> public PVector add() { myArray.add(new PVector(x,y,z)); }

change to

```auto
 public PVector add(PVector pv) {
    myArray.add(pv);
  }

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 10:57am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/7 "2020-04-20T10:57:45Z")

</div>

Also remove the fields x, y an z from the queue class as they have nothing to do with the class

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 11:01am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/8 "2020-04-20T11:01:03Z")

</div>

It still gives an error:

> This method must return a result of type PVector

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 11:02am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/9 "2020-04-20T11:02:33Z")

</div>

> [@noel](#):
>
> This method must return a result of type PVector

Why do you want the add method of a queue to return a PVector?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 11:03am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/10 "2020-04-20T11:03:56Z")

</div>

Two options

```auto
public void add(PVector pv) {
  myArray.add(pv);
}

```

and

```auto
public PVector add(PVector pv) {
  myArray.add(pv);
  return pv;
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 11:04am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/11 "2020-04-20T11:04:19Z")

</div>

The first one makes sense for a queue.

Also to keep the terminology right for a queue I would rename the methods  
`add -> push`  
and  
`remove -> pop`

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 11:18am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/12 "2020-04-20T11:18:46Z")

</div>

Thanks!!  
The second one worked.

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 1:19pm UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/13 "2020-04-20T13:19:58Z")

</div>

> [@quark](#):
>
> Why do you want the add method of a queue to return a PVector?

I’ve added a working Rosetta task [here](http://www.rosettacode.org/wiki/Bitmap/Flood_fill#Processing).  
I want to add this code to OpenProcessing. Since imports there don’t work I’m trying to solve it this way. But maybe I’ve the whole Queue thing wrong, because now I can’t pass

> if (test\_tolerance) println(“Passed test\_tolerance”);

which has nothing to do with the Queue, and works perfectly well in the original code. I hope you can look into this code, because my head is spinning. (An image [here](https://drive.google.com/file/d/1pEfJzMg_IjFWQtnmFV1SOQmeEAWFT0YR/view).)

```auto
PImage img;
int tolerance;
color fill_color;
boolean allowed;

void setup() {
  size(600, 400);
  img = loadImage("image.png");
  fill_color = color(250, 0, 0);  
  fill(0, 0, 100);
  tolerance = 15;
  image(img, 0, 0, width, height);
  textSize(18);
  text("Tolerance = "+tolerance+" (Use mouse wheel to change)", 100, height-30);
  text("Right click to reset", 100, height-10);
}

void draw() { 
  if (allowed) {
    image(img, 0, 0, width, height);
    text("Tolerance = "+tolerance+" (Use mouse wheel to change)", 100, height-30);
    text("Right click to reset", 100, height-10);
    allowed = false;
  }
}

void mousePressed() {
  img.loadPixels(); // A continious bitmap array with no reference to width
  flood(mouseX, mouseY);
  img.updatePixels(); 
  allowed = true;
  if (mouseButton == RIGHT) img = loadImage("image.png");
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  tolerance += 2*e;
  if (tolerance > 256) tolerance = 256;
  if (tolerance < 0) tolerance = 0;
  allowed = true;
}

void flood(int x, int y) {
  color target_color = img.pixels[pixel_position(mouseX, mouseY)];   
  boolean[][]p_control = new boolean[img.height][img.width];
  PVectorQueue queue = new PVectorQueue();
  float i = 0;
  queue.add(new PVector(i, i));
  while (!queue.isEmpty()) {
    println(queue.peek());
    PVector p = queue.remove();
      if (check(p_control, int(p.x), int(p.y), target_color)) {     
      println("check");
      queue.add(new PVector(p.x, p.y-1)); 
      queue.add(new PVector(p.x, p.y+1)); 
      queue.add(new PVector(p.x-1, p.y)); 
      queue.add(new PVector(p.x+1, p.y));
      //println(queue.peek());
    }
  }
}

int pixel_position(int x, int y) { // Returns position of clicked coordinates width / height of the image 
  return x + (y * img.width);
}

boolean check(boolean[][]p_control, int x, int y, color target_color) {
  if (x < 0 || y < 0 || y >= img.height || x >= img.width || p_control[y][x]) return false;
  int pp = img.pixels[pixel_position(x, y)];
  boolean test_tolerance = (abs(green(target_color)-green(pp)) < tolerance
                         && abs( red(target_color)- red(pp)) < tolerance
                         && abs( blue(target_color)- blue(pp)) < tolerance);
  if (test_tolerance) println("Passed test_tolerance");
  if (!test_tolerance) return false; 
  img.pixels[pixel_position(x, y)] = fill_color;
  p_control[y][x] = true;
  return true;
}

public class PVectorQueue {
  int front; 
  ArrayList<PVector> myArray = new ArrayList<PVector>();

  PVectorQueue() {
    front = 0;
  }

  public PVector add(PVector pv) {
    myArray.add(pv);
    return pv;
  }

  public PVector remove() {
    PVector res = myArray.get(front);
    myArray.remove(0);    
    return res;
  } 

  public PVector peek() {
    if (isEmpty()) {
      println("UnderFlow\nProgram Terminated");
      exit();
    } 
    return (PVector) myArray.get(0);
  }

  public int size() {
    return myArray.size();
  }

  public Boolean isEmpty() {
    return (size() == 0);
  }
}

```

---

<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:** [April 20, 2020, 1:58pm UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/14 "2020-04-20T13:58:02Z")

</div>

Here’s a more streamlined FiFo Queue container I’ve come up w/ just now: 😇  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html)

```auto
/**
 * ArrayQueue (v1.1.1)
 * GoToLoop (2020/Apr/20)
 * https://Discourse.Processing.org/t/linkedlist-vs-arraylist/19946/14
 */

//package java.util; // Uncomment this line if this is a ".java" file

import java.util.Collection;
import java.util.Queue;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.Serializable;

static // Comment this out if this is a ".java" file
  public class ArrayQueue<E> implements Queue<E>, Cloneable, Serializable {

  //static final long serialVersionUID = 8_779_387_929_858_209_947L;
  static final long serialVersionUID = 779_387_929;

  protected final List<E> list;

  public ArrayQueue() {
    list = new ArrayList<E>();
  }

  public ArrayQueue(final int initialCapacity) { // Ignored by Pjs
    list = new ArrayList<E>(initialCapacity);
  }

  public ArrayQueue(final Collection<? extends E> c) {
    list = new ArrayList<E>(c);
  }

  public void clear() {
    list.clear();
  }

  public int size() {
    return list.size();
  }

  public boolean isEmpty() {
    return list.isEmpty();
  }

  public boolean offer(final E e) {
    return add(e);
  }

  public boolean add(final E e) { // No return (void) on Pjs
    return list.add(e);
  }

  public E peek() {
    return isEmpty()? null : element();
  }

  public E element() {
    return list.get(0);
  }

  public E poll() {
    return isEmpty()? null : remove();
  }

  public E remove() {
    return list.remove(0);
  }

  public boolean remove(final Object o) {
    return list.remove(o);
  }

  public boolean removeAll(final Collection<?> c) {
    return list.removeAll(c);
  }

  public boolean addAll(final Collection<? extends E> c) { // void on Pjs
    return list.addAll(c);
  }

  public boolean retainAll(final Collection<?> c) { // Undefined on Pjs
    return list.retainAll(c);
  }

  public boolean containsAll(final Collection<?> c) { // Undefined on Pjs
    return list.containsAll(c);
  }

  public boolean contains(final Object o) {
    return list.contains(o);
  }

  public Object[] toArray() {
    return list.toArray();
  }

  public <T> T[] toArray(final T[] a) { // Parameter ignored by Pjs
    return list.toArray(a);
  }

  public Iterator<E> iterator() {
    return list.iterator();
  }

  public ArrayQueue<E> clone() {
    return new ArrayQueue<E>(list);
  }

  public String toString() { // Undefined on Pjs
    return list.toString();
  }
}

```

> <https://github.com/processing-js/processing-js/blob/master/src/Objects/ArrayList.js>

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 20, 2020, 2:00pm UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/15 "2020-04-20T14:00:15Z")

</div>

I think the problem is that with the demise of applets, Open Processing does not accept Java sketches anymore, they need to be written with Javascript. I haven’t used OP for years so I suggest you ask someone more knowledgeable regarding OP and JS. @GoToLoop can you advise noel?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 20, 2020, 5:27pm UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/16 "2020-04-20T17:27:33Z")

</div>

Now that’s a really nice decent detailed class.  
I have it working fast and smooth in the IDE.  
On OpenProcessing it does not work, however.  
but also doesn’t give any error. There must be a way. OpenProcessing is such a good place to show examples.  
See [here.](https://www.openprocessing.org/sketch/877125)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 20, 2020, 8:05pm UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/17 "2020-04-20T20:05:40Z")

</div>

> [@quark](#):
>
> Open Processing does not accept Java sketches anymore, they need to be written with Javascript.

Yes, I think the issue may be that its Java syntax sketches are backed by Processing.js. So you don’t need to write them as JavaScript, but that is how they have to run…

---

<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:** [April 21, 2020, 7:10pm UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/18 "2020-04-21T19:10:34Z")

</div>

> [@noel](#):
>
> On OpenProcessing it does not work, however.

It works for me for some reason. 🤔

In order to test ArrayQueue, I’ve picked 2 old sketches using container ArrayDeque as a Queue: ⛏  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayDeque.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayDeque.html)

And then replaced that w/ my own ArrayQueue, and both sketches were still perfectly OK:👌

> <https://gist.github.com/GoToLoop/571eaa4797963ffbacdd706542c30637>
>
> There are more than three files. show original

> **[Trailing Deque \[Java/Pjs\] - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/878488)**
>
> Processing/Pjs Cross-mode ArrayQueue container demo

> <https://gist.github.com/GoToLoop/145418d1dfb062c7c41a630bce5b8374>
>
> There are more than three files. show original

> **[Brownian Motion \[Java/Pjs\] - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/878458)**
>
> https://en.Wikipedia.org/wiki/Brownian\_motion

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 22, 2020, 7:41am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/19 "2020-04-22T07:41:25Z")

</div>

@GoToLoop  
The Brownian Motion is absolutely beautiful. I already had seen and downloaded it when I was searching for fifo’s. It has such a nice spatial feeling. The Fill Flood works also with your class, but I did not post a link because I haven’ t found a solution yet for a really weird problem. When I run it with my normal monitor settings it plays problematic, not fluid, and not reacting sometimes. Comparing the OP’s window size with the size of the P5 window, when running there, I saw that it the OP’s window is bigger, so I set my google´s browser zoom setting to 80%, and then the sketch plays almost as good and fast as if it were on P5. Later on, I will try to solve it with PGraphics.See [here.](https://www.openprocessing.org/sketch/878781)  
Edit: when placing a direct link in this post, it doesn’t play at all,

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [April 22, 2020, 11:16am UTC](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/20 "2020-04-22T11:16:56Z")

</div>

Of course, PGraphics doesn’t work as well because it takes the mouseX / Y system variable.  
My next idea is to detect the devicePixelRatio (which is the actual problem), that can be done in js code as showed [here.](http://tombigel.github.io/detect-zoom/) When set to 1.0 with zoom. the sketch runs well. Then I would ask the user to temporarily adjust it to play the code. But honestly, there must be another solution, because I suspect that this will happen in P5.js mode as well.  
@quark @GoToLoop @jeremydouglass @poboyd @anyone?

[Next page](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946.md?page=2)
