开发者

String [][] clarification

开发者 https://www.devze.com 2023-02-18 17:33 出处:网络
I\'m just asking about this instruction: String[][] s开发者_如何学运维 = new String[2][2]; If i create this variable \"s\", will i create a string table with 2 lines and 2 columns ?

I'm just asking about this instruction:

String[][] s开发者_如何学运维 = new String[2][2];

If i create this variable "s", will i create a string table with 2 lines and 2 columns ? Or 2 tables with 2 elements? Thank you for this clarification.


It creates a two-dimensional array with an array of two elements in both dimensions.

Depending on the sole purpose of the variable and the data it actually holds, you're free to interpret the whole whatever way you want. A table (a matrix) with 2 columns and 2 rows is perfectly fine.


Java has no build-in table datatype, the only native basic types of types (metatypes?) are

  • primitive values (numerics, char and boolean)
  • arrays (linear indexable collections of variables of the same type).
  • objects of classes (basically a collection of named variables of specified type, together with some methods, fixed by class). (Strings are examples of this.)

Everything else must be composed of those types. (To complicate this, the array types are all considered subtypes of java.lang.Object, which is the class from which all other classes inherit.)

So you can have arrays of a specific class, classes which have array-types as class variables (fields), and also arrays of arrays, as here.

Your String[][] type consists of arrays of arrays of String, and the new String[2][2] array creation expression creates an array of length two, each element being itself an array of length two. Each element of these arrays can be a string, but on creation it is first null.

 s --> [ 0 ,  1 ]
         |    '----> [ null, null ]
         |
         '---------> [ null, null ]

You now can put in the places of these nulls references to actual String objects, if you want, by using s[0][1] = "Hello"; and similar statements.

As said already by the other answerers, such a 2D-array can be viewed as a table with rows and columns, if you want, but it is nothing special from the language viewpoint.


I think that you are hung up about which is the one true explanation.

In reality, they are are many correct explanations or conceptual models. Which of these is most appropriate / makes the most sense depends on the conceptual level your are thinking at.

  • At the level of the language / JVM specification you have an array of arrays of strings.

  • At the syntactic level you have something that looks like a 2-D array of strings, with columns and rows. For a lot of operations it will behave exactly like a 2-D array.

    (You can do things to make your String[][] not behave like a conventional 2-D array. For example, you can replace a row to give an "array" in which the rows have different lengths. But that only happens if you "assign" a whole row.)

  • At the application level what you have can be thought of as a table ... if you want to.

The explanation that you linked to in one of your comments is correct, and so are all of the answers. They are all saying (or trying to say) the same thing.

(BTW - this is what @BalusC's answer is saying. I'm just elaborating.)


So when you ask:

If i create this variable "s", will i create a string table with 2 lines and 2 columns ? Or 2 tables with 2 elements?

... the answer is that it is BOTH of those, depending on your perspective, and how your application uses them. From other perspectives, it is also an array of arrays of strings, or a 2-D array of strings.


You are creating a two dimensional array. This is an array of arrays.


This will technically create an array - of length 2 - of string arrays - also of length 2.

But for all practical purposes it can be thought of as creating a table with 2 rows and 2 columns.


There is no table.

There is an array, an array is simply a reference to the memory where the variables are actually stored.

it will actually create a matrix which is an array of which each element is an array... array one: [] [] array two: [] []


This is a 2D array. just like writing

String [][] s = new  String[2][];
s[0] = new String[2];
s[1] = new String[2];

So I think both your options are correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消