# I have Some trouble with a ArrayList

**URL:** https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863
**Category:** Coding Questions
**Created:** [September 19, 2022, 3:43pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863 "2022-09-19T15:43:54Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![CreCo](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CreCo](https://discourse.processing.org/u/CreCo)
#### Post date: [September 19, 2022, 3:43pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/1 "2022-09-19T15:43:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 19, 2022, 4:18pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/2 "2022-09-19T16:18:41Z")

</div>

@CreCo  
Did I interpret the following correctly?

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

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

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

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

```

with lines like:

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

```auto
List<C1> yourList=monitoredList(new ArrayList<C1>());

```

---

<div class="post-metadata">

### Author: ![CreCo](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CreCo](https://discourse.processing.org/u/CreCo)
#### Post date: [September 19, 2022, 5:08pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/3 "2022-09-19T17:08:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 19, 2022, 5:12pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/4 "2022-09-19T17:12:37Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![CreCo](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CreCo](https://discourse.processing.org/u/CreCo)
#### Post date: [September 19, 2022, 5:16pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/5 "2022-09-19T17:16:04Z")

</div>

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”

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 19, 2022, 5:17pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/6 "2022-09-19T17:17:49Z")

</div>

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

---

<div class="post-metadata">

### Author: ![CreCo](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CreCo](https://discourse.processing.org/u/CreCo)
#### Post date: [September 19, 2022, 5:25pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/7 "2022-09-19T17:25:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 19, 2022, 5:26pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/8 "2022-09-19T17:26:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![CreCo](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CreCo](https://discourse.processing.org/u/CreCo)
#### Post date: [September 19, 2022, 5:34pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/9 "2022-09-19T17:34:23Z")

</div>

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

```

}

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 19, 2022, 5:52pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/10 "2022-09-19T17:52:40Z")

</div>

> [@CreCo](#):
>
> for(int x = 0; x \< 3; x = x + 1)  
> {  
> MakeRandomPoints();  
> }

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

Another thing would be replacing List with

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

---

<div class="post-metadata">

### Author: ![CreCo](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CreCo](https://discourse.processing.org/u/CreCo)
#### Post date: [September 19, 2022, 6:10pm UTC](https://discourse.processing.org/t/i-have-some-trouble-with-a-arraylist/38863/11 "2022-09-19T18:10:53Z")

</div>

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.
