Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

creating 2d array of strings  RSS feed

 
Preethi Vallam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Marshal
Posts: 4257
256
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's actually 3×3×2. But I am not quite sure.
 
Preethi Vallam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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




 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Norm Radder
Rancher
Posts: 2036
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
We can walk to school together. And we can both read this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!