Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# creating 2d array of strings

Greenhorn
Posts: 9
Hi,

I have a 2d array of strings, which are having "X","Y","Z" in the column and rows. Basically, I have:

String["X"]["X"], String["X"]["Y"], String["X"]["Z"]
String ["Y"]["X"],String ["Y"]["Y"],String ["Y"]["Z"]
String ["Z"]["X"],String ["Z"]["Y"], String ["Z"]["Z"]

Basically, a 3*3 matrix. The values for these are strings. How is this best represented in Java?

Thanks,
Preethi

Marshal
Posts: 59108
180

Preethi Vallam wrote:Hi,

I have a 2d array of strings . . .

No, you haven't because Java® doesn't support 2D arrays. That is an array of arrays, which is better than a 2D array. Actually you appear to have three levels of nesting of arrays in your diagram.
I don't understand your question about how that is best represented.

Sheriff
Posts: 5694
393

Preethi Vallam wrote:I have a 2d array of strings, which are having "X","Y","Z" in the column and rows.

Preethi Vallam wrote:Basically, a 3*3 matrix.

You mean 3 * 3 * 3 ?

Perhaps an actual example would help to clarify better.

Campbell Ritchie
Marshal
Posts: 59108
180
I think it's actually 3×3×2. But I am not quite sure.

Preethi Vallam
Greenhorn
Posts: 9

str1str2str3
XYZ
str1XY
Zstr2X

This is my data. Here, str1, str2,str3, X,Y,Z are all strings. Also, 1st row is str1, 2nd row is str2, 3rd row is str3.

Campbell Ritchie
Marshal
Posts: 59108
180
That doesn't make sense. You appear to have those objects srt1 srt2 and srt3 as headings for the columns.
Getting the Strings into arrays of arrays doesn't make sense. You would need some sort of titled triplet<String> object. And you can't create an array of that, but you can put it into a List.

Rancher
Posts: 3067
33
For me That post shows a table with 4 rows of three columns.
The first row has the values: "str1", str2" and "str3".
The first column has the values: "str1", "X", "str1" and "Z"

Preethi Vallam
Greenhorn
Posts: 9
The ones that are marked in bold in the table are the headers - so, instead of referring to the table with integer indexes like Array[1][2], I am using strings. Basically, my first row is having values for:
Array["str1"]["str1"]=X,
Array["str1"]["str2"]=Y,
Array["str1"]["str3"]=Z

Second row is having:
Array["str2"]["str1"]=str1,
Array["str2"]["str2"]=X,
Array["str2"]["str3"]=Y

Third row is having:
Array["str3"]["str1"]=Z,
Array["str3"]["str2"]=str2,
Array["str3"]["str3"]=X

Rancher
Posts: 3067
33

referring to the table with integer indexes like Array[1][2], I am using strings.

What happens when you try to compile that code?

The indexes to arrays are int values, not Strings.

Preethi Vallam
Greenhorn
Posts: 9
I haven't written any code to store this data yet. I just wanted to know how to store this kind of data.

Thanks
Preethi

Rancher
Posts: 3067
33

how to store this kind of data.

This expression that you posted look like it would work:
Array[1][2]
follow it with an = and the value that should be assigned to the slot in the array

Campbell Ritchie
Marshal
Posts: 59108
180
Those nested arrays still look like a bad design to me. I still think OP needs a Triplet class. Or even a Matrix class, which would permit separate fields for headers and for content.

 What are your superhero powers? Go ahead and try them on this tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss