aspose file tools
The moose likes Beginning Java and the fly likes phonebook and array? help? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "phonebook and array? help?" Watch "phonebook and array? help?" New topic
Author

phonebook and array? help?

devin panchal
Greenhorn

Joined: Apr 18, 2004
Posts: 3
hi,
i need some help creating a phonebook (array).
ok, heres the code wat i got so far;
Phonebook class
public class PhoneBook
{
private int arraySize;
private Contact[] array;
public PhoneBook(int maxArraySize)
{
array = new Contact[maxArraySize];
arraySize =0;
}
public void addAContact(String name, int number)
{
Contact theContact = new Contact (name, number);
array[arraySize] = theContact;
arraySize++;
}
private int findContact(int arrayNumber)
{
int location = -1;
for(int i=0; i<arraySize; i++)
{
if(array.getArrayNumber() == arrayNumber)
{
location = i ;
}
}
return location;
}
//public void incAppearance(int arrayNumber)
//{
// array[findContact(arrayNumber)].addToAppearances();
//}
public void incGoals(int goals, int arrayNumber)
{
array[findContact(arrayNumber)].addToGoals(goals);
}
public void displayArray()
{
System.out.println("Player Played Scored\n");
for(int i=0; i<arraySize; i++)
{
System.out.println(array.getName()+"\t "+array.getAppearances()
+"\t\t"+array.getGoalsScored());
}
}

}
ignore the end bit, its has been added from my other group member. i edited this one, and got the above to compile.
Contact class
public class Contact
{
private String name;
private int arrayNumber;
private int goalsScored;
private int appearances;
public Contact(String aName, int aNumber)
{
name = aName;
arrayNumber = aNumber;
}
public String getName()
{
return(name);
}
public int getArrayNumber()
{
return(arrayNumber);
}
public int getAppearances()
{
return(appearances);
}
public int getGoalsScored()
{
return(goalsScored);
}
public void addToGoals( int score)
{
goalsScored += score;
}
}
ok. thats wat i got so far. it holds names and numbers, but i want to store them in an array. my other group memebers r wokring on other bits. i'm try to create an array, so;
-a new entry can be added to the phonebook,
- entry can be deleted, modifyed, etc.
- a search function. (which is the main bit)(using name and/or number)
please void the appearances, goal scores, as i have not implemented that code. i have not had time to edit that bit.
wat else can i do to make the code better? more effecient? make it a proper array?
thanx
dev
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm not sure what you are asking. As far as I can tell, you have all the correct syntax to declare an array and initialize it:

You also seem to have the code to correctly add information to the array:

So what exactly are you having problems with here? I don't see anything obviously incorrect. It looks like you understand how to use arrays.
If I'm missing something here, please clarify your question. Include compiler errors, if any, as well as the output you get and an explanation of how it differs from what you expect.
Keep coding!
Layne


Java API Documentation
The Java Tutorial
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Realize, when using an array (indicated with []), you will need to define the size of the array at compile time. So in essence, you will need to determine the maximum size (as you did with the parameter maxArraySize). This is not always the best solution because:
.
  • make the size too large, and you'll be wasting space
  • make it too small, and your users may get pissed when they run out of room in their phonebook
  • you'll need to "null" out empty indexes so you know which elements are empty
  • eventually your array will resemble a piece of Swiss cheese (certain elements will be empty, others full)
  • because of the "Swiss cheese" effect, you'll have to do a sequential search for an empty element every time you want to add a new object (you won't be able to simple add to the "end" anymore because the "end" may be occupied)

  • A better implementation would be to use a Hashmap (see the Java Collections API). Whereas the traditional array[] requires a sequential search (starting at 0 and going up to size - 1), the Hashmap allows you to perform a search in constant time. Also, adding and removing elements are simple operations. It would be helpful if you understand the basics of hashing, but it is not entirely essential.
    [ April 19, 2004: Message edited by: Jeffrey Hunter ]
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
  • you'll need to "null" out empty indexes so you know which elements are empty

  • This is one of several possible solutions. The code above opts to use the variable arraySize variable instead to keep track of which elements are used and the position to insert the next element.
  • eventually your array will resemble a piece of Swiss cheese (certain elements will be empty, others full)
  • because of the "Swiss cheese" effect, you'll have to do a sequential search for an empty element every time you want to add a new object (you won't be able to simple add to the "end" anymore because the "end" may be occupied)

  • This situation, also called fragmentation, is avoided in the above code since there is no "remove" operation in the above code.
    Jeffrey Hunter
    Ranch Hand

    Joined: Apr 16, 2004
    Posts: 305
    a new entry can be added to the phonebook,
    - entry can be deleted, modifyed, etc.
    - a search function. (which is the main bit)(using name and/or number)

    A indexing variable alone is insufficient to add an element to an array suffering from the "Swiss cheese" effect. Eventually the indexing variable will reach the end of the array, and you'll need some mechanism to identify where the next free slot is.
    Joel McNary
    Bartender

    Joined: Aug 20, 2001
    Posts: 1815
    Originally posted by Jeffrey Hunter:
    Realize, when using an array (indicated with []), you will need to define the size of the array at compile time.

    While this is true in C, Java does not have this restriction. You are perfectly capable of creating arrays of dynamic size -- which is what is done above. The size of the array is passed in as a variable.
    I do agree with your analysis that a Map would be the better implementation, however -- unless, of course, if this is a school project and the instructor is expecting an array....


    Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by Jeffrey Hunter:

    A indexing variable alone is insufficient to add an element to an array suffering from the "Swiss cheese" effect. Eventually the indexing variable will reach the end of the array, and you'll need some mechanism to identify where the next free slot is.

    I guess the original poster hasn't implemented the deletion/removal operation from the requirements. My comments were referring to the code. You are correct that such issues need to be considered in order to implement this operation.
    devin panchal
    Greenhorn

    Joined: Apr 18, 2004
    Posts: 3
    hi,
    thanx for all the replies. i appreciate the help.
    thanx
    devin
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: phonebook and array? help?
     
    Similar Threads
    Why does this"if" statement get skipped?
    Array problem
    Getting grades into a gradebook
    Searching an Array
    arrays & sorting arrays