#How to concat() two arrays?

18 messages · Page 1 of 1 (latest)

vocal plinth
#

Hello friends 👋 I was here many years ago, disappeared for a while but am now back with a goal to learn Processing... and have gone all the way back to Coding Challenge 1 - Starfields!

I've been adding to the code myself (successfully converted from 2D to 3D 🎉 ) but have hit a snag wanting to add more stars while the sketch is running. I decided (perhaps incorrectly) that a good way to do this would be to create a small array of more Star objects, and concat() the two arrays when a key is pressed. But even troubleshooting in setup() I can't join these two arrays together.

Here's the relevant code. Why am I getting the error message Type mismatch: cannot convert from Object to cc_001_starfield_3d.Star[] ?

I've read and re-read the help page on concat() [https://processing.org/reference/concat_.html] but I don't understand why the example on that page String[] sa3 = concat(sa1, sa2); would work and the last line of my code here (which looks identical to me) doesn't? Can anyone explain/shed light? Why is Processing trying to convert? The objects are already the same type, right? aaaaaaaaah my brain! 🧠 🤯

Thank you in advance!

Star[] stars = new Star[400]; // an array to hold initial stars Star[] morestars = new Star[10]; // stars to be added each time "+" is pressed ... for (int i = 0; i < stars.length; i++) { stars[i] = new Star(); } for (int j = 0; j < morestars.length; j++) { morestars[j] = new Star(); } Star[] stars = concat(stars, morestars);

#

Maybe I should try to use append() ?
But if I copy/paste the example from the append() reference page:
String[] sa2 = append(sa1, "MA");
and substitute my array name etc to get:
Star[] stars = append(stars, new Star());
...I get the same error. Which I don't understand.

quiet pendant
#

concat() is a neat helper-function from processing and it is defined for booleans, bytes, chars, ints, floats, Strings and Objects.

#

So if you pass a Star[] array into it, it will choose the Object type and return the Object type.

#

(Your Star class inherits from Object, so it is also an Object.)

#

I think in this case, if you know you put Star objects in at one end, you should be safe to cast them back into Star later.

#

Basically, your type information ("I am a Star) is lost, but it still very much is the same object.

#

So that last line in your code would be Object[] stars = concat(stars, morestars); but this is not ideal.

#

and then you could cast back to Star like this Star currentStar = (Star) stars[3]; - I think. The (Star) is the typecast. Again: I am not 100% sure this works, and I am also not sure if there's a more elegant solution.

vocal plinth
#

Thanks for the help! I'm not sure I'm understanding still...
Object[] stars = concat(stars, morestars);
gives me
Type mismatch, “java.lang.Object” does not match with “java.lang.Object[]”
and
Object stars = concat(stars, morestars);
gives me
The local variable “stars” may not have been initialized

quiet pendant
#

ah, sorry... stars has been declared before

#

somewhere above that is already a Star[] stars ... line - meaning that the name stars is already taken.

#

but this would have been an error in your originally posted code from the first post, as well.

#

(I think?)

vocal plinth
#

In my original code, the error was the 'cannot conver' error. I've just tried using Object[] stars = and Object stars = but those don't work either.

stars HAS been initialized, and it's a Star[] object/array.

#

I'm going out to walk the dog so hopefully my brain will get some clarity... but I doubt it, this is all very confusing to me 🤣

#

Wait! I think I got it...

From https://processing.org/reference/concat_.html : When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) concat(array1, array2).

So...
tempstars = new Star[stars.length + morestars.length]; Star[] tempstars = (Star[]) concat(stars, morestars); stars = tempstars;

#

I have to set up an empty tempstars array of the combined length, before making the original stars array equal to that, otherwise Processing still complains that The local variable “stars” may not have been initialized ... I don't really understand this last error, but the above is now working! 🎉

I'll have to try to wrap my head around the idea of "casting" to a data type later!