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
SCJP
Visit my download page
Why should you be sorry?Barney Balboa wrote:Extremely sorry for digressing here . . .
Henry Wong wrote:
So... Yes. Your assumption is right. It will create "multi-dimensional" array -- in that it can be used that way.
Henry Wong wrote:
With "how to incorporate", that is just coding. You are changing the data structure, so you need to change your program.
Barney Balboa wrote:
Extremely sorry for digressing here.
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
Barney Balboa wrote:
Well, I thought maybe the original poster wasn't of any benefit from this. But thanks for the clarification
Campbell Ritchie wrote:
Why should you be sorry?Barney Balboa wrote:Extremely sorry for digressing here . . .
You see some non-object-oriented design and you are right to query the use of arrays.
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.Sudhir Srinivasan wrote: . . . . . .
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.
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.
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |