We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! Merry Christmas! In some tutorials and books examples, I've seen 2d arrays being initialized with only one pair of square brackets, although they have been declared as 2D array (with two pairs of square brackets) before. I've seen this when it is necessary to load a csv file and then split the values using a delimiter to separate them in rows for the 2D array, like this:
String ciudad[];
String datos[][];
void setup() {
ciudad = loadStrings("xxx.csv");
datos = new String[ciudad.length][8];
for (int i=0; i<ciudad.length; i++) {
datos[i] = csv[i].split(",");
}
}
I wonder why it is allowed to use a single pair of square brackets in the for loop, when it was previously declared (and later, used) as a 2d array. Maybe, "that's the way it is", but I guess there is a more reasonable explanation. Why it doesn't ask for the values for the second square bracket or just show an error message for the missing pair?
Thanks, and Happy New Year!
Answers
Some observations about your posted code:
However, you attempt to access it by the name csv inside the
for ( ; ; )
loop! @-)[]
are suffixed after the variable name, is frowned upon. :-\" So instead ofString datos[][];
, go w/String[][] datos;
B-)datos = new String[ciudad.length][];
;;)Hi! Thanks for your fast response.
1- My mistake. This piece of code was just an example, and I had changed some names to make it more readable for me, but forgot to change that "csv" for "ciudad".
2- I had always that doubt about where to put the sq brackets, because I've seen them written both ways. Thanks for the clarification.
3-And finally, thanks for the explanation. Very clear. I didn't find it in Processing tutorials or books, so I guess, I should have explored about Java a little bit. Thanks again. You are very helpful. Best regards, Marcelo
By the way, it's interesting the way you code the for loop. I mean, you put "println(words)" in the same line as the "for", instead of inside curly brackets, and also, without any separation (semicolon or whatever). Thanks!
if
,else
,for
,while
&do
always expect at least 1 statement following it.;
. >-)Thanks, GoToLoop! for the explanations and the tutorials! It really seems I have to get into Java, after all (well, I know Processing is Java...)
The way you have written code very interesting and I hope the links below will be helpful for you :-*
http://www.flowerbrackets.com/java-array/ http://www.flowerbrackets.com/two-dimensional-array-java-program/
Thx @samrogerwilliams. ^:)^