Solved

Using a 2D array in Java

Posted on 1997-08-05
4
736 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
servlet filter example 37 83
Tomcat: Unable to run tomcat service. 2 29
Bot application - advice 3 61
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

749 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