Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

creating 2d array of strings  RSS feed

 
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
 
Marshal
Posts: 60101
188
  • 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.
 
Marshal
Posts: 6008
415
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: 60101
188
  • 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: 60101
188
  • 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.
 
Master Rancher
Posts: 3276
33
  • 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
Master Rancher
Posts: 3276
33
  • 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
Master Rancher
Posts: 3276
33
  • 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: 60101
188
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!