LinkedList vs ArrayList

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?

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

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?

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

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

Change to

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

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)

change to

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

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

It still gives an error:

This method must return a result of type PVector

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

Two options

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

and

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

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

Thanks!!
The second one worked.

1 Like

I’ve added a working Rosetta task here.
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.)

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

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

/**
 * 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();
  }
}
1 Like

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?

1 Like

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.

1 Like

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…

1 Like

It works for me for some reason. :thinking:

In order to test ArrayQueue, I’ve picked 2 old sketches using container ArrayDeque as a Queue: :pick:
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::ok_hand:



1 Like

@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.
Edit: when placing a direct link in this post, it doesn’t play at all,

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