• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multi dimensional arrays

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I thought maybe the original poster wasn't of any benefit from this. But thanks for the clarification
 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic