wood burning stoves*
The moose likes Beginning Java and the fly likes Passing a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing a function" Watch "Passing a function" New topic
Author

Passing a function

Rick Katka
Greenhorn

Joined: Feb 22, 2004
Posts: 7
I'm new to Java ~2 weeks.
I just wrote a contacts class that holds all of my contacts for a phonebook and I need to write an add function in my Phonebook class. I am unsure of how to pass this if it is even possible at all. I have already taken in the values of the contact and have them stored in an array. Now I just need to create the add function to put the contacts into a different array.
Thanks
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
why do you use arrays?
Did you consider using HashMap, for example?
There are a couple of nice and convenient collection
classes out there in the Java API.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Originally posted by Rick Katka:
I'm new to Java ~2 weeks.
I just wrote a contacts class that holds all of my contacts for a phonebook and I need to write an add function in my Phonebook class. I am unsure of how to pass this if it is even possible at all. I have already taken in the values of the contact and have them stored in an array. Now I just need to create the add function to put the contacts into a different array.
Thanks

I'm afraid I don't understand your question. Please could you rephrase and it would also help if you posted your code.
D.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
just create a method in your phonebook class that looks something like this:
public Object addToPhoneBook(Object x){ //this will pass in every value in from the phone book and it will return an object.
Object [] temp = new Object[x];
//myCt is your counter from the other class
for (int i = 0; i < yourArry.length; i++) {
temp[i]=x; //every object that comes in will get added to the new array
myCt++; //increment counter for capacity of array
}
yourArry = temp; //set YourArry to the temp array
return yourArry;
}
if your array is of type int, then do the same with int instead of oject. Instantiate the an instance of the phonebook class in the contacts class and call the addToPhoneBook method and pass in the returned value from the contacts class. This will add your values into a new array(that you setup) in the phonebook class.
Instantiate it like this:
phoneBook p1 = new phoneBook();
p1.addToPhoneBook(???whatever your passing in???);
HTH
[ February 27, 2004: Message edited by: Gabriel White ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
why do you use arrays?
Did you consider using HashMap, for example?
There are a couple of nice and convenient collection
classes out there in the Java API

Juliane, this may be a little to advanced for someone that is new to java.
 
 
subject: Passing a function
 
Similar Threads
Reading input from console to 2D array with Scanner Class
Updateable JList
Project Help
phonebook and array? help?
Project Help