aspose file tools*
The moose likes Java in General and the fly likes Multi dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multi dimensional arrays" Watch "Multi dimensional arrays" New topic
Author

Multi dimensional arrays

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90

Hi,

My program dimensions the student number, takes input of the student names[displayed in rows] & marks and returns the student marks, total & average
[row-column] for the given 5 subjects[displayed in columns]. The output displays student-wise (marks in all 5 subjects for each student) total / average and
subject-wise (marks of all the students in each subject) total / average.

I've used a combination of single and two dimension arrays to store and return the values. Attached below is the entire code.

(Do excuse me for copy pasting the entire code on to the web page as it was unavoidable........did try attaching the source code & its output as as txt or word doc but couldn't do
so as only image files are allowed)



and its output for

Student-wise total and average


and

Subject-wise total and average




I would like to reduce the program length (too many for loops) by using rectangular multi-dimensional arrays, in this case, array of array of arrays, say

int[][][] result = new int[studnum][subject][studmarks];

where [studnum] is an array of rows,
[subject] is a sub-array of the previous array i.e to store the length of each row, length being same
and [studmarks] is a sub-array of [studnum] and [subject].

Is my assumption right? If yes, I would need help on 2 counts:
1) how to incorporate multi-dim arrays having same length sub-arrays and as an enhancement
2) ---ditto--- having sub-arrays of varying lengths

Could any of the forum experts help in getting me started (explanation accompanied by the code snippet would be of great help!).

thanks,
Sudhir


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Sudhir Srinivasan wrote:
I would like to reduce the program length (too many for loops) by using rectangular multi-dimensional arrays, in this case, array of array of arrays, say

int[][][] result = new int[studnum][subject][studmarks];

where [studnum] is an array of rows,
[subject] is a sub-array of the previous array i.e to store the length of each row, length being same
and [studmarks] is a sub-array of [studnum] and [subject].

Is my assumption right? If yes, I would need help on 2 counts:
1) how to incorporate multi-dim arrays having same length sub-arrays and as an enhancement
2) ---ditto--- having sub-arrays of varying lengths


As already mentioned, Java supports "array of array of arrays", which is not really "multi-dimensional arrays". So, this syntax...



is just syntactic sugar. The Java compiler will instantiate the array of array of array object, and then loop through it to create the array of array objects, and then loop through those to create the array objects. Lot of work from a single line of code, I say.


So... Yes. Your assumption is right. It will create "multi-dimensional" array -- in that it can be used that way.


As for your last two questions...

With "how to incorporate", that is just coding. You are changing the data structure, so you need to change your program.

With "arrays of varying lengths", you simply can't instantiate that way. You have to instantiate the main array like this...



And then loop through it yourself to instantiate the individual arrays. Basically, you have to do the work, that in the first case, the compiler did for you.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Extremely sorry for digressing here.

Why would one want to go for multidimensional arrays? Collections, especially Maps, provide such a wonderful mechanism of (property, value) pairings that it just seems wrong to me that we assign some meaning to something totally abstract like arr[0][1][2]. You might know what you are fetching but it takes a lot of time for someone to ascertain the matrix and find out the value. For a simple set up like Subject, Student, Marks obtained, you can easily go for Map of Map where it is easier (atleast looks easier) to demarcate each level. Simple 1-dimensional arrays are absolutely fine, but why go for more?

The day i learnt how to use Collections, there was no looking back on arrays
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

one advantage of 2d arrays at least is that you can use Points to refer to the elements. can be very handy and easy to use.


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Barney Balboa wrote:Extremely sorry for digressing here . . .
Why should you be sorry?

You see some non-object-oriented design and you are right to query the use of arrays.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Well, I thought maybe the original poster wasn't of any benefit from this. But thanks for the clarification
Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90


At the outset, thank you to all the responders.

Henry,

Thanks for the validation.

Henry Wong wrote:
So... Yes. Your assumption is right. It will create "multi-dimensional" array -- in that it can be used that way.


Will modify the program

Henry Wong wrote:
With "how to incorporate", that is just coding. You are changing the data structure, so you need to change your program.


and revert for further clarifications.


Barney,

The reason for posting on this forum
Barney Balboa wrote:
Extremely sorry for digressing here.

is to elicit diverse responses and most certainly welcome anything that adds to my knowledge of java (& in turn to other visitors of this forum).

Could you please provide the link(s)
Barney Balboa wrote:
Collections, especially Maps, provide such a wonderful mechanism of (property, value) pairings that it just seems wrong to me that we assign some meaning to something totally abstract like arr[0][1][2]. You might know what you are fetching but it takes a lot of time for someone to ascertain the matrix and find out the value. For a simple set up like Subject, Student, Marks obtained, you can easily go for Map of Map where it is easier (atleast looks easier) to demarcate each level. Simple 1-dimensional arrays are absolutely fine, but why go for more?

The day i learnt how to use Collections, there was no looking back on arrays

that helped you to learn java collections (including maps) framework.

I'm not sure what you mean - assuming you meant I wasn't going to benefit from your answer - but would certainly benefit from any (valid) alternative
Barney Balboa wrote:
Well, I thought maybe the original poster wasn't of any benefit from this. But thanks for the clarification

to my original post. Otherwise, I wouldn't be here if I thought my approach is the right one.

thanks,
Sudhir
Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90

Hi Campbell,

As always, your response is

Campbell Ritchie wrote:
Barney Balboa wrote:Extremely sorry for digressing here . . .
Why should you be sorry?

You see some non-object-oriented design and you are right to query the use of arrays.


cryptic. Why isn't multi dimensional arrays the right choice. Isn't the statement



the right way to instantiate, in this case, an array of array of array int object. How is it non-object oriented? Could you please elaborate. Also, could you provide a link to any javaranch article relating to collections.

thanks,
Sudhir
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
You ought to have a Student object. As fields of that class, you can have marks. You can have an array of marks for each student, and you can put students into arrays. Or, as Barney Balboa has already told you, Lists of whatever.
You ought to have the Java Tutorials bookmarked already. You will find a whole section about Collections in there. That is worth reading, and if you have anything you don’t understand, you can ask again.

Using arrays of arrays of arrays in simply a way to get all confused.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Sudhir Srinivasan wrote: . . . . . .
You would need to write down what each of those indices means. You would have each student taking the same number of subjects, and each subject has exactly the same number of marks. You have now got a very rigid structure which you cannot readily alter. Think how much easier it would be to use Student and Subject objects.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Campbell :
You ought to have a Student object. As fields of that class, you can have marks. You can have an array of marks for each student, and you can put students into arrays. Or, as Barney Balboa has already told you, Lists of whatever.


Although I was thinking in a much cruder way, I'll stick with what you say

Just for closure, this was what I was thinking - Lets say you had a structure like this instead of the array - Map<String, Map<String, Integer>> which would yield a (Student Name, Map of (Subject, Marks)). With this structure,

1) I can query based on Students name and obtain marks of each subject. The query is intuitive because there are no indices and you would have to work on the keys.
2) You can perform out of the box operations like sort, compare, iterate etc.
3) You can plug this structure into a bigger map, let say, of the Class that the Student belongs to.
Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90
Campbell Ritchie wrote:You ought to have a Student object. As fields of that class, you can have marks. You can have an array of marks for each student, and you can put students into arrays. Or, as Barney Balboa has already told you, Lists of whatever.


I think I've got what you're getting at. I'll modify the program and get back to you.

Campbell Ritchie wrote:
You ought to have the Java Tutorials bookmarked already. You will find a whole section about Collections in there. That is worth reading, and if you have anything you don’t understand, you can ask again.


Thanks for the link. It certainly requires a bit of reading before its implementation in the program. Will certainly ask in case of doubt(s).

thanks and regards,
Sudhir

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
You’re welcome
 
Don't get me started about those stupid light bulbs.
 
subject: Multi dimensional arrays