• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays of arrays of arrays question  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Long time C programmer, comfortable in C++, started learning Java yesterday.

My main data set is a 2d array of chars. I have several of them, so I thought I'd make another array dimension. Works fine until I start to pass data to methods. Ex:



I realize I'm probably DoingItWrong, so how do I DoItRight? I suspect the right way is to make my 2d data a class, then have an array of that class. Sorta like a C struct
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
snot nose wrote:Long time C programmer, comfortable in C++, started learning Java yesterday.

My main data set is a 2d array of chars. I have several of them, so I thought I'd make another array dimension. Works fine until I start to pass data to methods. Ex:



I realize I'm probably DoingItWrong, so how do I DoItRight? I suspect the right way is to make my 2d data a class, then have an array of that class. Sorta like a C struct



The declaration is incorrect. The allData variable should not be a char type -- it should be a char [][][] type.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
snot nose wrote:My main data set is a 2d array of chars. I have several of them, so I thought I'd make another array dimension. Works fine until I start to pass data to methods. Ex:
I realize I'm probably DoingItWrong, so how do I DoItRight?

Yes, you're doing it wrong, but:
I suspect the right way is to make my 2d data a class, then have an array of that class. Sorta like a C struct

is NOT the way to solve it. You're over-engineering.

Imagine that instead of being a fixed array, your 'allData' variable was defined as a char***. How would you solve the problem then in C/C++?

The structure of a char[][][] in Java is much more like a C char*** than a C char[][][], except that there's no such thing as malloc(). You create subarrays with 'new char[]..[]'.

You can create Java arrays as in your example, but you will then have created a static structure (and, very likely, one that uses far more memory than it needs).

HIH

Winston
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Imagine that instead of being a fixed array, your 'allData' variable was defined as a char***. How would you solve the problem then in C/C++?


That's pretty much what I'm doing

Fixing the data declaration got me further, now I have issues converting arrays of chars to a String

What the hay, I'll post the code here while I search the web for my problem.



Better algorithms are much appreciated as well, I'm new to this stuff.

Actually, what I would really like to do is create my dataset as an array of Strings. Caveat is Strings are immutable, and I need to change them. Not change their length, but change, say, "foo" to "bar". My array of chars is a workaround for this problem.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Fixing the data declaration got me further, now I have issues converting arrays of chars to a String

Why? Unless that array of chars IS a String, in which case you can use the String constructor that takes a char[]. Otherwise StringsAreBad.

Winston
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I pointed out the line of code causing me grief.


Just discovered StringBuffers. I want to figure out my current problem, then I'll look into using StringBuffers instead of an array of chars. Prolly not tho, my data really is a 2d array of chars.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:I pointed out the line of code causing me grief.


Just discovered StringBuffers. I want to figure out my current problem, then I'll look into using StringBuffers instead of an array of chars. Prolly not tho, my data really is a 2d array of chars.



The compiler is complaining that your class (name Grid) doesn't have a method named String(), that takes a single char[] as a parameter -- so you can't make such a method call.... perhaps you meant to instantiate a String object instead?

Henry
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm trying to instantiate a String object out of data[y]. data is declared char [][], so char[y] should be a char [], shouldn't it?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Yes, I'm trying to instantiate a String object out of data[y]. data is declared char [][], so char[y] should be a char [], shouldn't it?


Instantiating an object in Java is very similar to how it is done in C++, you have to use the new operator.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Yes, I'm trying to instantiate a String object out of data[y].

Erm...did you not read my post? It seems to me like you're just machine-gunning questions.

Programming is NOT an existentialist art, it's a technique. And it requires thought.

My 2nd piece of advice: StopCoding (←click).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!