Solved

Using a 2D array in Java

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now