• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multidimensioanl Arrays  RSS feed

 
Devin Walton
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I've been working on this problem for a week and I haven't made any headway on it. The general idea behind it it that you have to print off a table with 10 columns and three rows. In one column you have names, the other you have to print off their over risk for high cholesterol, and in the third you have to print off their risk of high ldl cholesterol. The third column will call a method, but that's a whole other issue. The problem I'm running into is that I can't get the second column to print. I'm looking for some pointers here and maybe a push in the right direction, I'm not looking for a direct solution. So, here's my code and I would appreciate some feedback on it, thanks
[Edit by Winston: Broke up that very long line 21]
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will that program compile?
You are declaring int[][] arrays, which are not multidimensional. Java only supports 1‑dimensional arrays. What you are declaring is arrays of arrays. Anyway, you might declare arrays of arrays, but you are assigning those references to ordinary arrays.
You should not have a ROWS or COLUMNS field. You should get the length of an array out of the array, via its .length field. That way you can have an 11‑element array and still get all its elements.
You have misunderstood the format of the printf arguments. You should have one String with all the % tags in, followed by a comma‑separated list of arguments. You should not need tab characters, because you can use the numbers after % instead. Never use the linefeed character \n. Use the %n tag instead.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused about what the table should look like, as I feel you're mixing up columns and rows somewhere in your explanation.
Is this what you're looking for:

Or this:


In any case, you should think of a "multi-dimensional array" as an array whose elements are other arrays. So when you declare an array like

What this means is: give me an array that stores arrays of int values, and make it one that can store 3 arrays which in turn can store 10 int values.

What you're doing in you code is something different, and illegal:

This means, give me an array that stores arrays of int values and make it one that doesn't store arrays of int values, but these specific int values instead. That doesn't make any sense to the compiler.

What would make this legal is to change the syntax to the following:

Simply surround the int values with curly brackets, and the compiler will know to treat them as an array initializer, meaning you'll en up with an array of arrays that each hold exeactly one int value.

At any rate, while it's certainly possible to represent a table as an array of arrays, it wouldn't be my first choice in this case, as you need to store different data types (int's and String's). Is the use of a "multi-dimensional array" a requirement of the assignment?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devin Walton wrote:The general idea behind it it that you have to print off a table with 10 columns and three rows. In one column you have names, the other you have to print off their over risk for high cholesterol, and in the third you have to print off their risk of high ldl cholesterol...

Well, all these arrays seem to refer to a specific thing - a Patient - so why not write a Patient class?

Then you could gather all that separate information into a single array, viz:or alternatively, simply set it up from scratch:
Now you only have one array to worry about, and it contains objects that define exactly what you want - and that's how Object-Orientation works.
(well, part of it )

HIH

Winston
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be doing 4 separate arrays and your code only needs to be changed a little:

 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This puts the readings into a mutli-dimensional array:
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This puts all the data into a multi-dimensional array:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!