Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing a function

 
Rick Katka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic