One pair of square brackets in 2D arrays?

edited December 2016 in Questions about Code

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!

Tagged:

Answers

  • edited December 2016 Answer ✓

    Some observations about your posted code:

    1. You declare a String[] 1D array called ciudad at the top.
      However, you attempt to access it by the name csv inside the for ( ; ; ) loop! @-)
    2. Although it's also valid in Java, the C array style declaration, where the [] are suffixed after the variable name, is frowned upon. :-\" So instead of String datos[][];, go w/ String[][] datos; B-)
    3. When declaring multi-dimensional arrays, only the outer dimension demands its length upfront. The others are optional: datos = new String[ciudad.length][]; ;;)
  • edited December 2016 Answer ✓
    String[][] sentences;
    sentences = new String[3][]; // 2D assignment
    
    String[] words0 = { "This", "is", "a", "1D", "array" };
    sentences[0] = words0; // 1D assignement
    
    String[] words1 = { "I", "am", "a", "1D", "array", "which", "stores", "strings" };
    sentences[1] = words1;
    
    String[] words2 = { "We", "can", "be", "of", "diff.", "lengths", "too" };
    sentences[2] = words2;
    
    for (String[] words : sentences)  println(words);
    println();
    
    // Another 2D assignment passing all 1D arrays at the same time:
    sentences = new String[][] { words1, words2, words0, new String[] {"Wut?!", "O_o"} };
    for (String[] words : sentences)  println(words);
    
    exit();
    
  • edited December 2016

    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

  • edited December 2016

    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!

  • edited December 2016 Answer ✓
    • if, else, for, while & do always expect at least 1 statement following it.
    • As long it's only 1, curly brackets are optional. :ar!
    • And if we don't want any statement after them, finish them w/ an empty ;. >-)
    • P.S.: No need for the only statement be in the same line. We can move it below. :P
  • edited December 2016 Answer ✓

    I didn't find it in Processing tutorials or books.
    So I guess, I should have explored about Java a little bit.

    1. https://www.TutorialsPoint.com/java/java_arrays.htm
    2. http://www.Java67.com/2014/10/how-to-create-and-initialize-two-dimensional-array-java-example.html
  • 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/

Sign In or Register to comment.