# ArrayList of ArrayLists (dynamic 2D array) of custom class

**URL:** <https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923>\
**Category:** Beginners\
**Created:** [February 19, 2021, 6:26am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923 "2021-02-19T06:26:32Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 19, 2021, 8:12pm UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/21 "2021-02-19T20:12:19Z")

</div>

Yes this can get confusing so here’s a link explaining that :

> **[Reference Variable in Java - GeeksforGeeks](https://www.geeksforgeeks.org/reference-variable-in-java/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 19, 2021, 8:28pm UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/22 "2021-02-19T20:28:37Z")

</div>

Hmmm …  
I need some time to think about these things …  
I’ll let you know if I hve more questions …

(I can see now why some people have a “distaste” for java …)

---

<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:** [February 20, 2021, 12:10am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/23 "2021-02-20T00:10:37Z")

</div>

> [@ProgrammingN00b](#):
>
> (I can see now why some people have a “distaste” for Java …)

If how reference behaves under Java was the reason why Java isn’t liked much folks would also hate JavaScript, Python and almost all modern programming languages! 😳

Actually programmers tend to dislike Java b/c we have to explicitly declare the datatype of each single variable just like old C/C++ do! 😱

BtW I’ve got some previous posts about Java’s reference aliases below: ☕

> [@Boolean trouble or something](https://discourse.processing.org/t/boolean-trouble-or-something/16217/5):
>
> Variables/fields/parameters store a single value. They range from 1 byte to 8 bytes in most languages. At the statement m = new PVector(); the operator new creates a contiguous memory block enough to fit in all PVector’s non-static fields. And when it’s done, it returns the 1st memory address of that block. And finally, that 1st memory address is stored in the field m as a reference value. Now, when you do pressLoc = m, you’re assigning field pressLoc w/ the exact value of m. After that assign…

> [@Final array is being changed](https://discourse.processing.org/t/final-array-is-being-changed/1447/4):
>
> Some of my clone() examples I did a long time ago at the link below may help ya out: smile_cat[https://Forum.Processing.org/two/discussion/12044/how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array](https://Forum.Processing.org/two/discussion/12044/how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array) final int[][] stuff = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; final int[] myArray = stuff[2]; // it's an alias for stuff[2]. myArray[0] = 10; // thus it alters stuff[2] too! println(stuff[2][0]); // prints out 10. stuff[2] is also affected! final int[] myClone = stuff[2].clone(); // it's a clon…

> [@Final array is being changed](https://discourse.processing.org/t/final-array-is-being-changed/1447/6):
>
> The 1st example explains at its comments why the 2nd 1 works. wink Processing got nothing to do w/ that, but the Java language itself! coffee As you know by now, the 1st example already got those basic insights. roll_eyes Basically, we can have more than 1 variable (and array slots) pointing at the same object. They simply become alias for the same object. flushed That is, if we mutate an object via 1 of its alias, its other aliases see those changes as well, b/c they all store th…

> [@How do I pass a function by reference and then call it?](https://discourse.processing.org/t/how-do-i-pass-a-function-by-reference-and-then-call-it/27474/3):
>
> Actually JS functions, just like Java’s, are ALWAYS pass by value, regardless it’s a primitive or object datatype. When we pass a variable as a single argument to a function, the current value stored in that variable is read, and then the invoked function’s receiving parameter is initialized w/ a copy of that read value. B/c a function’s parameter can only receive a copy of the current value stored in a passed variable, reassigning another value to that local parameter won’t replace the value…

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 20, 2021, 12:15am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/24 "2021-02-20T00:15:33Z")

</div>

> [@GoToLoop](#):
>
> If how reference behaves under Java was the reason why Java isn’t liked much folks would also hate JavaScript, Python and almost all modern programming languages!

Nice point here!

@ProgrammingN00b Java is relatively easy compared to C++ believe me 😋

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 5:17am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/25 "2021-02-20T05:17:01Z")

</div>

Well, I am just very much used to Var1 = Var2 only “copying” the value of Var2 into Var1 …

---

<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:** [February 20, 2021, 5:29am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/26 "2021-02-20T05:29:14Z")

</div>

> [@ProgrammingN00b](#):
>
> … only “copying” the value of Var2 into Var1 …

That’s exactly the assign operator `=` does:

It replaces the value of the variable from the left side w/ a copy of the value from the right side.

But depending on the left variable’s datatype that value is interpreted differently.

If it’s a primitive datatype that value is interpreted as a number.

Otherwise it’s interpreted as the 1st memory address of an object’s contiguous memory block.

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 6:08am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/27 "2021-02-20T06:08:59Z")

</div>

Yes, that is the weird part, because C or C++ does not do that …

---

<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:** [February 20, 2021, 6:12am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/28 "2021-02-20T06:12:59Z")

</div>

Can you explain how C/C++ variable assigning is different than Java’s then? 🤔

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 6:19am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/29 "2021-02-20T06:19:18Z")

</div>

`
Variable1 = 5;
Variable2 = 10;
Variable1 = Variable2;
//The value of Variable1 would now be 10
Variable2 = 20;
//Setting the new value of variable2 to 20
`

`//Variable1 would still be 10 and not change as Variable2 gets changed
`

---

<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:** [February 20, 2021, 6:25am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/30 "2021-02-20T06:25:30Z")

</div>

That’s exactly what happens in Java too! So I dunno what’s your point. 😵

```auto
int var1 = 5, var2 = 10;
println(var1, var2); // 5, 10

var1 = var2;
println(var1, var2); // 10, 10

var2 = 20;
println(var1, var2); // 10, 20

exit();

```

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 6:27am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/31 "2021-02-20T06:27:19Z")

</div>

But in C++ this would copy the “value” no matter what type the variable is!  
So even if it is a custom class / object …  
Or a member variable of an object / class …

---

<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:** [February 20, 2021, 6:35am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/32 "2021-02-20T06:35:50Z")

</div>

> [@ProgrammingN00b](#):
>
> But in C++ this would copy the “value” no matter what type the variable is!

By “copy” you actually mean “clone”.

That is, rather than assigning a copy of the same value from right to left C/C++ would clone an object, thus generating a new value, which is then assigned to the left variable.

In Java cloning isn’t done automatically. We’d need to code a function for it.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [February 20, 2021, 6:39am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/33 "2021-02-20T06:39:43Z")

</div>

Usually if i have to deal with copies, ill use something which can store an object usually an arraylist. I use one for the original and a second one for all copies.

Then i iterate through the first arraylist and based on the conditions im looking for ill code an appropriate logic statement and set an index object in the second arraylist or empty the arraylist and add the desired object.

Not necessarily as direct as in other languages but it works.

Im sure there might be better ways though.

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 6:50am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/34 "2021-02-20T06:50:34Z")

</div>

Ok, I guess I will try to create a function to “clone” values from one variable to another …

(That’s what I get for saying “this processing stuff is easier then c++”, oh well …)

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 6:57am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/35 "2021-02-20T06:57:56Z")

</div>

That is probably some good advice, but I don’t understand enough of this yet to fully comprehend it …  
😀

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 8:09am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/36 "2021-02-20T08:09:22Z")

</div>

I just found this article:

> **[Comparison of Java and C++](https://en.wikipedia.org/wiki/Comparison_of_Java_and_C++)**
>
> This is a comparison of Java and C++, two prominent object-oriented programming languages.
> The differences between the programming languages C++ and Java can be traced to their heritage, as they have different design goals.
> C++ was designed for systems and applications programming (i.e. infrastructure programming), extending the procedural programming language C, which was designed for efficient execution. To C, C++ added support for object-oriented programming, exception handling, lifetime-b...

It esplains the differences between c++ and java.

---

<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:** [February 20, 2021, 9:06am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/37 "2021-02-20T09:06:26Z")

</div>

This is that wiki’s relevant part: 🤓

 ![JavaC](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bd8ed8568b0e9d3d23384e8035dde91277c51c46.png)

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 10:09am UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/38 "2021-02-20T10:09:30Z")

</div>

But it appears that that function variable.clone() does not exist in processing …  
😞

---

<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:** [February 20, 2021, 12:43pm UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/39 "2021-02-20T12:43:09Z")

</div>

Look again what’s stated on the wiki screenshot I’ve posted earlier:

```auto
// the Foo class must implement the Cloneable interface
// for this code to compile

```

As I’ve warned you before we’ve gotta code our own **clone()** function.

Here’s an example of how to implement a **clone()** method within a class:

> [@Functions argument](https://discourse.processing.org/t/functions-argument/11010/5):
>
> Or even better, use it as an actual full-fledged class, w/ constructors, fields & methods: triumph // https://Discourse.Processing.org/t/functions-argument/11010/5 // GoToLoop (v1.1) (2019/May/09) final Pair pair = new Pair(5, -10); void setup() { println(pair); // [5, -10] println(pair.calc('y')); // [-50, 540] println(pair.set(5, -10)); // [5, -10] println(pair.calc('u')); // [3, -2] println(pair.clone()); // [3, -2] exit(); } static final class…

---

<div class="post-metadata">

**Author:** ![ProgrammingN00b](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@ProgrammingN00b](https://discourse.processing.org/u/ProgrammingN00b)\
**Post date:** [February 20, 2021, 4:27pm UTC](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923/40 "2021-02-20T16:27:16Z")

</div>

I think found a better solution:  
Something like “processing” but for c++!  
[https://openframeworks.cc/](https://openframeworks.cc/)

[Previous page](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923.md?page=1)

[Next page](https://discourse.processing.org/t/arraylist-of-arraylists-dynamic-2d-array-of-custom-class/27923.md?page=3)
