Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Newbie questions - Arrays and methods

Thread view: 
Navin R. Johnson - 10 Apr 2004 17:41 GMT
Hello again,

First, thanx for reading his... I'm taking my first Java class and up
'til a week ago, I've understood almost everything. We started working
with arrays and methods and now I'm flabbergasted - especially with
passing arrays to and from methods. I think I understand how an array is
an object and is not actually passed to a method - just the reference to
the array is passed. I have no problem creating arrays and filling in
the array elements. I know how to size an arrray using input from the
user, how to manipulate the data within an array and how to determine
the 'length' of an array.

First question - can more than one array be passed to a method? And if
so, can two different types of arrays be passed at the same time - like
an int array and a String array? What's the syntax used to do this?

Second - if I create an array within a method, can that array then be
passed back to the main method? How??

The reason I'm asking this is due to a homework assignment. We're
supposed to write a program that displays grades for a number of
students, shows the high, the low and the average grade. The number of
students is initially unknown and the program will ask the user to input
the number. After that it will ask for the names of the students. I want
to accomplish this using two arrays - one (int type) to hold the grades
and one (String type) to hold the names. We're required to use methods
that pass values via the parameter list. I created an int array in
'main' to hold the student grades by using a method that asks the user
for the number of students. I then created a String array in main using
'array.length' to find the size, which will hold all the names. The two
arrays are both the same size. I get lost here though. I want to use a
method that first asks the user for the names of each student then asks
the user for each student's grade - example: What is the name student
number 1?... Moe... What is Moe's grade?... 85 etc. What I can't figure
out is how to pass the references for both arrays to the method that
asks for the names and the grades??? If I create the second array
(String type to hold the names) within the method, how do I get that
array back to main?

I hope someone here understands what I'm asking. I'm totally new to this
Java stuff so I'm not even sure this makes sense to anyone but me. Any
help would be greatly appreciated.

Thanks,
Navin R. Johnson

"The new phone books are here!"
Christophe Vanfleteren - 10 Apr 2004 17:51 GMT
> Hello again,
>
[quoted text clipped - 11 lines]
> so, can two different types of arrays be passed at the same time - like
> an int array and a String array? What's the syntax used to do this?

Yes:

public void method(int[] intArray, String[] stringArray)

> Second - if I create an array within a method, can that array then be
> passed back to the main method? How??

public String[] method() {
       String[] stringAr = ...
       //do stuff with stringAr
       return stringAr;
}

<snip assignments>

But isn't all this mentioned in your textbook somewhere?

If you don't have a textbook, you can always try the java tutorial at
http://java.sun.com/docs/books/tutorial/

Signature

Kind regards,
Christophe Vanfleteren

Navin R. Johnson - 10 Apr 2004 18:41 GMT
>> Hello again,
>>
><snip>

>> First question - can more than one array be passed to a method? And if
>> so, can two different types of arrays be passed at the same time - like
[quoted text clipped - 3 lines]
>
>public void method(int[] intArray, String[] stringArray)

Thanx. I thought that might work but I had the syntax all wrong.

>> Second - if I create an array within a method, can that array then be
>> passed back to the main method? How??
[quoted text clipped - 7 lines]
>
>But isn't all this mentioned in your textbook somewhere?

No book - just a very waaay out instructor.... Ask a question and you
get a 30-min war story along with just a partial answer.... The sun site
is great but it always seems to take me an hour to find answers to
specific questions and I'm really pressed for time right now. Thanx
again.

>If you don't have a textbook, you can always try the java tutorial at
>http://java.sun.com/docs/books/tutorial/
Roedy Green - 11 Apr 2004 05:17 GMT
>> so, can two different types of arrays be passed at the same time - like
>> an int array and a String array? What's the syntax used to do this?
>
>Yes:
>
>public void method(int[] intArray, String[] stringArray
the call would look like this:
 int[] intArray = new int[4];
 String[] stringArray = new String[3];
 ... some initialisation ...
 method( intArray, stringArray );

method is free to change the elements of both arrays, but it can't
make either variable point to a different object and have the caller
notice. In other words it can't change the size of either array. The
best it could do is return a reference to a new bigger array.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Steve Horsley - 10 Apr 2004 18:41 GMT
> Hello again,
>
[quoted text clipped - 7 lines]
> user, how to manipulate the data within an array and how to determine
> the 'length' of an array.

Good. KNowing that only the _reference_ is passed is critical.

> First question - can more than one array be passed to a method? And if
> so, can two different types of arrays be passed at the same time - like
> an int array and a String array? What's the syntax used to do this?

You can pass any number of arguments to a method. In your case, I guess
you want to have a method like:
   public void myMethod(int[] grades, String[] names) {
       ...

> Second - if I create an array within a method, can that array then be
> passed back to the main method? How??

You can return one value from any method. This can be an array reference
as easily as anything else:

   public int[] makeIntArray() {
       int[] array = new int[10];
       //  fill it with values
       return array;
   }

   ...
   int[] values = makeIntArray();

> The reason I'm asking this is due to a homework assignment. We're
> supposed to write a program that displays grades for a number of
[quoted text clipped - 22 lines]
> Thanks,
> Navin R. Johnson

Possibly, a problem you keep running in to is ther need to handle
both arrays all the time. You have already said that both arrays will
be the same size. You seem to be anticipating a difficulty in returning
two arrays from a method call. If you ever want to sort the arrays,
by name or by grade, you will have difficulty in keeping them in sync.

The cause of these problems is that you are not grouping the information
together in the right way. I would suggest that you consider creating
a class Student, which has a name and a grade. Then you are in a better
position to answer problems like "Which student has the highest grade?".
And you only have one array to worry about - an array of Students.
Like this:

   Student[] students = getStudentInfo();

getStudentInfo() can ask how many students, then loop getting
names and grades,and return one array.

This mentality is beginning to think about the problem (students
with grades) rather than the data (names and grades), if that makes sense
to you, and is a more object-oriented approach.

This may actually be preempting your class syllabus though - that may be
next week's lesson. I don't want to derail a well structured course.

Steve
Roedy Green - 11 Apr 2004 05:19 GMT
>Second - if I create an array within a method, can that array then be
>passed back to the main method? How??

int[] method()
{
int[] x = new int [3];
  ... initialisation...

return x;
}

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.