Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using a 2D array in Java

Posted on 1997-08-05
4
Medium Priority
?
740 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 200 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

610 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