• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing a function

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic