Sizeof array in processing

Hi,

I am using Processing to send lots of data do arduino but sometimes my code crashes and I discovered it's because I am using more itens of my array than I have.

In my code I have this:

String[][] val = {{"0","6","10"},{"1","10","10"},{"1","10","14"},{"1","10","14"},{"1","10","14"}};

How do I get to know that this "val" array has 5 elements in its first level? I tried using sizeOf(val) but processing says "The function sizeOf does not exist". I also tried "sizeof" and didnt work either.

I dont want to explicit the number of itens in my array at String[10][], so how I do that?

PS: I am aware that sizeOf will return the number of bytes not of elements, but I can work it out by myself after I get the number of bytes.

String[][] val = {{"0","6","10"},{"1","10","10"},{"1","10","14"},{"1","10","14"},{"1","10","14"}};

(sizeof val / sizeof val[0])                  //  This should evaluate to 5
(sizeof val[0] / sizeof val[0][0])        // This should evaluate to 3

You have to divide by the element size because 'sizeof' gives the size in bytes and you want the size in elements.

Thank your for helping me into this. I am using Processing (not arduino) and when I run your code I get this error -> http://quemfazsite.com.br/temp/aaa.jpg

Try:-

sizeof(val) / sizeof(val[0])

(Braces around the parameter.)

Thanks, but same error "the function sizeof" does not exist :frowning:

batata004:
Thanks, but same error "the function sizeof" does not exist :frowning:

I'm not too familiar with Processing, only Arduino, but thought it was worth a try. That's how it's done with C++. (John just forgot the braces.)

Perhaps the Processing forum is the place to ask?

I just did a quick test using Processing3. This prints 5:-

String[][] val = {{"0","6","10"},{"1","10","10"},{"1","10","14"},{"1","10","14"},{"1","10","14"}};
int i = val.length;
println(i);

And this prints 3:-

String[][] val = {{"0","6","10"},{"1","10","10"},{"1","10","14"},{"1","10","14"},{"1","10","14"}};
int i = val[0].length;
println(i);

Edit: No need to go to the Processing forum after all. :smiley:

Edit2: After a bit more playing, I found that this returns the length of an individual element:-
(The first element of the first array in this case.)

String[][] val = {{"0","6","10"},{"1","10","10"},{"1","10","14"},{"1","10","14"},{"1","10","14"}};
println(val[0][0].length());

Note the parentheses after 'length' in this example. They're required.)

@OldSteve it worked perfectly! I could not find any documentation how to get the size of array using processing, but your help was perfect.

I am just wondering why println(val.length()) does not work and outputs an error in processin.

batata004:
@OldSteve it worked perfectly! I could not find any documentation how to get the size of array using processing, but your help was perfect.

I am just wondering why println(val.length()) does not work and outputs an error in processin.

It's just the way the language is put together.
'length' is a variable, 'length()' is a function, each with it's own job to do.

'println(val.length)' will work.

@OldSteve wow it worked! Indeed, using () is calling a function and without it I am reading directly a value.

Thank you so much, now I can move on till finding the next problem :slight_smile:

batata004:
@OldSteve wow it worked! Indeed, using () is calling a function and without it I am reading directly a value.

Thank you so much, now I can move on till finding the next problem :slight_smile:

Glad to help, and I learned something too. I haven't played with Processing much.

@OldSteve I started playing with it yesterday and without your help I would be stuck for one more day with this problem.

Thank you, have a nice weekend :slight_smile:

batata004:
I could not find any documentation how to get the size of array using processing

Processing is using Java, like Arduino uses C++.