• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array of object in phonebook  RSS feed

 
diaa baraka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am required to implement a program to represent a phonebook. The program is required to handle all the common features of phonebooks such as insertion, deletion, editing and searching.This is my first project in college and i'm confused alittle .
first question :
how to make an array of object whose size are not fixed?
i mean that i want to make its size equal the number of elements not larger
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to make an array of object whose size are not fixed?

You can't make an array which is not fixed size but you can use ArrayList which is essentially an array who's size expands as required.

i mean that i want to make its size equal the number of elements not larger

If you know in advance how many elements you will have (which is difficult for an address book unless your teacher has told you to only allow for x number of people) you can was an array as follows:
Object[] myArray = new Object[5];

Where Object is the class name of the type of array you want and '5' is the number of elements you want.
 
diaa baraka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no its size isn't fixed so what's solution in this case(phone book)?
sorry if i'seemed very beginner
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an ArrayList.
 
diaa baraka
Greenhorn
Posts: 12
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch BTW, sorry that's a bit late.

How does the link to the other thread explain your problem - that code uses a fixed size array.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought for a phone book or similar, you naturally use a Map.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought for a phone book or similar, you naturally use a Map.


Let him get comfortable with arrays and lists before throwing map at him.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If he can use Lists, he can learn to use a Map in 5 minutes. If he only uses arrays, we ain’t gonna teach him to use a List here.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diaa baraka wrote:no its size isn't fixed so what's solution in this case(phone book)?
sorry if i'seemed very beginner



Well, assuming it is a requirement of the assignment, in that it must use arrays... then you have no choice, just pick a size for the fixed array (as best that you can). If you pick a size that is too large, then some space will be wasted. If you pick a size that is too small, then you need code that will allocate a larger array, copy all the entries from the previous array to it, and then continue using the larger array.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!