Solved

Using a 2D array in Java

Posted on 1997-08-05
4
734 Views
Last Modified: 2011-10-03
Hopefully my questions are finally a bit less "Newbiesque."  Or is the term "Newbile?"

I was going through a book of Java(1.1) exercises the other day, and the section on arrays suggested that I make a gradebook using a 2D Java array.  Each student gets a number, and a series of test scores.  So I get that I have to do something like:

int gradeBook[][] = new int gradeBook[25][10]

That's 25 students x 10 tests, right?  A few questions arise, which the book didn't explain so well.  First, in linking together the two arrays, can I mix the types?  Like have the first array be a string and the second be an int, so that I could refer to the students by name and the scores by int?  

Second, how do I set the value of an item in a multi-dimensional array?  The chapter gives an example setting a point on a grid represented by a 2D array to a BOOLEAN true (gridPoint[4][6] = true;), but that doesn't really tell how to set the value of each point in the array separately.  Or am I confused?  

Third, how do I initialize the student score arrays without having to enter each test separately?  I tried doing something like:
gradeBook[0] = {88, 99, 97, 87, 83, 78, 92, 89, 93, 92};
(e. g. setting the second array of student 0 to reflect these scores) but Java wasn't having any of that.

For help I turned to another book which stated flat-out that "Java doesn't support multi-dimensional arrays."  But I don't think the first book would have proposed an exercise for which there is no solution.

Thanks!

Raydot.  
0
Comment
Question by:Raydot
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 50 total points
ID: 1224471
Java indeed does not support truly multidimentional arrays. It supports arrays of arrays instead.

Thus,
int gradeBook[][] = new int gradeBook[25][10]
is an array of int arrays. Or you have allocated an array of 25 int arrays of 10 elements each. Again, gradeBook is a single dimentional array but its elements are arrays.
No, you cannot mix the types the way you proposed. Then you are better of using hastable, where the array of grades is linked to a student's name. here each student is linked to an index of its grade array, that is gradeBook[13] is an array of grades of 13th student.

For assignments you can do something like
int[][] gradeBook = { {1,2,3,4,5,6,7,8,9,0},
                      {0,9,8,7,6,5,4,3,2,1},
                      ....
                    }
or use the loop or do what you tried to do.

I hope that answers your question.


0
 
LVL 3

Author Comment

by:Raydot
ID: 1224472
Before I submit a grade, I'm wondering if you could just answer my second question, setting the value of an item in a multi-dimensional array.  So if I make a mistake on student 3's 8th test and need to change the grade from 21 to 91, how do I do it?  Is it:

gradeBook[2][7] = 91; ?

Thanks...
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1224473
You are absolutely correct. gradeBook[2] is in array of grades for the 3d student and gradeBook[2][7] is the grade for the 8th test.
0
 
LVL 3

Author Comment

by:Raydot
ID: 1224474
Thanks, msmolyak!  It just took a little nudge in the right direction, you know?  I think I understand it pretty well now.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question