23

I just had an idea to test something out and it worked:

    String[][] arr = new String[4][4];

    arr[2] = new String[5];

    for(int i = 0; i < arr.length; i++)
    {
        System.out.println(arr[i].length);
    }

The output obviously is:

4
4
5
4

So my questions are:

  • Is this good or bad style of coding?
  • What could this be good for?
  • And most of all, is there a way to create such a construct in the declaration itself?
  • Also... why is it even possible to do?
4

3 回答 3

26
  • Is this good or bad style of coding?

Like anything, it depends on the situation. There are situations where jagged arrays (as they are called) are in fact appropriate.

  • What could this be good for?

Well, for storing data sets with different lengths in one array. For instance, if we had the strings "hello" and "goodbye", we might want to store their character arrays in one structure. These char arrays have different lengths, so we would use a jagged array.

  • And most of all, is there a way to create such a construct in the declaration itself?

Yes:

char[][] x = {{'h','e','l','l','o'},
              {'g','o','o','d','b','y','e'}};
  • Also... why is it even possible to do?

Because it is allowed by the Java Language Specification, §10.6.

于 2013-08-01T15:06:13.887 回答
14
  1. This is a fine style of coding, there's nothing wrong with it. I've created jagged arrays myself for different problems in the past.

  2. This is good because you might need to store data in this way. Data stored this way would allow you to saves memory. It would be a natural way to map items more efficiently in certain scenarios.

  3. In a single line, without explicitly populating the array? No. This is the closest thing I can think of.

    int[][] test = new int[10][];
    test[0] = new int[100];
    test[1] = new int[500];
    

    This would allow you to populate the rows with arrays of different lengths. I prefer this approach to populating with values like so:

    int[][] test = new int[][]{{1,2,3},{4},{5,6,7}};
    

    Because it is more readable, and practical to type when dealing with large ragged arrays.

  4. Its possible to do for the reasons given in 2. People have valid reasons for needing ragged arrays, so the creators of the language gave us a way to do it.
于 2013-08-01T15:06:59.657 回答
1

(1) While nothing is technically/functionally/syntactically wrong with it, I would say it is bad coding style because it breaks the assumption provided by the initialization of the object (String[4][4]). This, ultimately, is up to user preference; if you're the only one reading it, and you know exactly what you're doing, it would be fine. If other people share/use your code, it adds confusion.

(2) The only concept I could think of is if you had multiple arrays to be read in, but didn't know the size of them beforehand. However, it would make more sense to use ArrayList<String> in that case, unless the added overhead was a serious matter.

(3) I'm not sure what you're asking about here. Do you mean, can you somehow specify individual array lengths in that initial declaration? The answer to that is no.

(4) Its possible to extend and shrink primitive array lengths because behind the scenes, you're just allocating and releasing chunks of memory.

于 2013-08-01T15:11:11.957 回答