Solved

Using a 2D array in Java

Posted on 1997-08-05
4
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

724 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