I have Some trouble with a ArrayList

Dear love Community,
I have some trouble with a ArrayList.
I have coded a little bit complex Program.
In this Program I have a set of Classes.
Of one of this Classes i have build a ArrayList.
Then I have coded three Methods about a Class which holds this ArrayList.
All this Methods give me no Errors, but in some chases the Code are working,
and in other chases the Program are doing nothing, and this without a Error Message.
My Code are to long to be written in this Post.
So I’m have no Idea how I can solve this Problem, or How I can get any help to
solve my Problem.
If there anybody out who has the power to hep me, it will be a great thing.

@CreCo
Did I interpret the following correctly?

You have a class C1 and a class C2 having a field of the type

ArrayList<C1>

C2 has a few methods to operate on this ArrayList.

If you want I can give you some code about monitoring ArrayLists!

Take a look at this code:

List<Integer> lst=new ArrayList<Integer>();

void setup() {
  lst=monitoredList(lst);
  for(int i=0;i<10;i++) lst.add((int) random(0,10));
  lst.get(0);
  lst.remove(3);
}

import java.util.*;
import java.lang.reflect.*;
<T> List<T> monitoredList(List<T> lst) {
  final List<T> ref=lst;
  InvocationHandler ih=new InvocationHandler() {
    public Object invoke(Object proxy, Method method, Object[] args) {
      try {
        Object ret=method.invoke(ref, args);
        String argstr=" ";
        for(Object argst:args) argstr+=argst+" ";
        println("Invoked: "+method.getName()+" Argument(s):"+argstr+" Result: "+ret);
        return ret;
      }
      catch(Exception e) {
        return null;
      }
    }
  };
  Object proxy=Proxy.newProxyInstance(PApplet.class.getClassLoader(),new Class[]{List.class},ih);
  return (List<T>) proxy;
}

If you want to use that yourself be sure to declare your ArrayLists as List<T> instead of ArrayList<T> so replace lines like:

ArrayList<C1> yourList=new ArrayList<C1>();

with lines like:

List<C1> yourList=new ArrayList<C1>();

be sure to import java.util though.
When that is done you can simply implement the monitoring by copying this code into your sketches code and replacing the before mentioned line with:

List<C1> yourList=monitoredList(new ArrayList<C1>());
1 Like

Correct I have declared a Class C1 and a Class C2.
On which C2 has a ArrayList.
So I must try it with the List myList = new ArrayList();
I think I can tell you tomorrow if it success or denied.

No this actually should change nothing however you then can use List<C1> myList=monitoredList(new ArrayList());

This will report your every action on the list making debugging easier:

PS: You do write it like List<C1> and not just use List right?

Ok, I get more trouble
I’m working on Processing 4,0b7.
And I have replaced
ArrayList allPoints = new ArrayList();
by
List allPoints = new ArrayList();

new I get the Error:
Cannot find a class or type named “List”

Write import java.util.*; at the beginning of your sketch.

Ok I have import java.util.*;
My Program will run with maximal 3 C1 in C2.
But for more C1 in C2 the Program starts and Does nothing

What do these 3 methods that operate on your ArrayList do?

I hope this excerpts will help

class PointsOfPosenenskeWeb{
// Declare the ArrayList allPoints to Hold all PointOfPosenenskeWeb Components
List allPoints = new ArrayList();

// Function to generate a single new PointOfPosenenskeWeb
void MakeRandomPoints(){
// Add PointOfPosenenskeWeb to ArrayList allPoints
allPoints.add(new PointOfPosenenskeWeb());
// Intialisate new PointOfPosenenskeWeb
int sice = allPoints.size();
allPoints.get(sice-1).CreateRandomlySpreadedPoint();
}

void InitalisePoints(){
for(int x = 0; x < 3; x = x + 1)
{
MakeRandomPoints();
}
}

// Draw all PointOfPosenenskeWeb from allPoints
void DrawPoints(){
// For Loop about all PointOfPosenenskeWeb to daraw
for(PointOfPosenenskeWeb pointxy : allPoints){
pointxy.Draw();
}
}
// BrownienMovement for all PointOfPosenenskeWeb from allPoints
void BrownienMovement(){
// For Loop about all PointOfPosenenskeWeb to get BrownienMovement
for (PointOfPosenenskeWeb point : allPoints) {
point.BrownienMovement();
}
}

and before

class PointOfPosenenskeWeb extends Point{
// Method to Construct the PointOfPosenenskeWeb
void CreateRandomlySpreadedPoint() {
super.X_Seter( int(random(0, width))); // Randomly Choose X Coordinate from Width of Canvss
super.Y_Seter(int(random(0, height))); // Randomly CHoose Y Coordinate from Heigt of Canvas
}

// Methode to draw the Point
void Draw()
{
circle(super.X_Geter(), super.Y_Geter(), 20); // Draw the Point as a White Circle
}

// Methode to get some BrownienMovement with the PointOfPosenenskeWeb
void BrownienMovement()
{
// Chache for new Coordinates of Point
int next_x = -11; // Set x Value to begining wrong Value
int next_y = -11; // Set y Value to begining wrong Value

// Speed Factor for Brownien Motion
float factor = 120; 

// While Loop to get a correct x Coordinate
while( (next_x < 12 ) || ( next_x > (width-12) ))
{
  next_x = int(super.X_Geter() + random(-(width/factor), (width/factor))); // Random Motion Equation
  
}

// While Loop to get a correct y Coordinate
while( (next_y < 12) || ( next_y >( height -12 )))
{
  next_y = int(super.Y_Geter() + random(-(height/factor), (height/factor))); // Random Motion Equation
  
}

// Seters for new correct Values
super.X_Seter(next_x); // Set new X Value
super.Y_Seter(next_y); // Set new Y Value

}

Is this the code where you set how many points you want to define?

Another thing would be replacing List with

List<PointOfPosenenskeWeb>

I don’t know how this is with higher Processing versions but in my case this won’t even run without.

Another thing would be checking for infinite Loops by adding a println in the draw section.
Using ungodly amounts of println commands is generally a good practice for debugging!

I would also recommend you try to learn to use constructors these can make your code easier to read and use.

Sorry, firstly this Method will call another Method who build the Points.
I have written this Function to become a Starting Point for my App.
The called Function then is:

void MakeRandomPoints(){
// Add PointOfPosenenskeWeb to ArrayList allPoints
allPoints.add(new PointOfPosenenskeWeb());
// Intialisate new PointOfPosenenskeWeb
int sice = allPoints.size();
allPoints.get(sice-1).CreateRandomlySpreadedPoint();
}

This latter Function should create one more Point. It will bee letter used to dynamically add new Points to the Set, and a - just not written - later Function should dynamically remove some Points.

1 Like