• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList - Contact Information/Address Book  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody, I have a question about arraylist objects.... I want to create an address book that will contain a persons information (name, phone, address, email..etc..). The first two snippets of code are from the same class ContactArrayList and the last one is from the class Contact. Do I need to create a separate arraylist for each data type? Is it possible to use a single arraylist to contain all the object (person) information and then be searched for using a GUI? I would also like to be able to access this data to be manipulated using a JFrame (add, edit, and search).





 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use a single arraylist to contain all the object (person) information and then be searched for using a GUI?


Well, kind of. You can use List<Object> and make it static, but I think this is a job for a small database and SQL. HSQLDB is an in-memory DB written in Java. Could be handy.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:Do I need to create a separate arraylist for each data type?

No, that is not how you normally work with lists. You can just use an ArrayList that contains Contact or ContactItem objects, a special class such as ContactArrayList is not necessary.

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really think a List is the best thing for an address book? If I were creating an address book I would take something which links Name→AddressAndPhoneNumber.

Have a look through the Java Tutorials and see if you can't find something else which links like that.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you really think a List is the best thing for an address book? If I were creating an address book I would take something which links Name→AddressAndPhoneNumber.

Have a look through the Java Tutorials and see if you can't find something else which links like that.


Well, I hear what you are saying, but it is an assignment for class...not a lot I can control in this case. haha
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Derek Nickerson wrote:Do I need to create a separate arraylist for each data type?

No, that is not how you normally work with lists. You can just use an ArrayList that contains Contact or ContactItem objects, a special class such as ContactArrayList is not necessary.



Unfortunately, I am unfamiliar with SQL and this is an assignment for a class, I must stick to the requirement prescribed by the teacher. I was planning to use the class ContactArrayList to contain all of the method details pertaining to the array.

Is it impossible to assign each object the personal data, which can later be retrieved and displayed within a JFrame?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not impossible at all. You know about Generics? How a list can have a type? Well, what type of list would you need? (Hint: you don't have to create another List class.)
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote: . . . Well, I hear what you are saying, but it is an assignment for class...not a lot I can control in this case. haha
If you found what I was thinking of, you have done well
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:It's not impossible at all. You know about Generics? How a list can have a type? Well, what type of list would you need? (Hint: you don't have to create another List class.)


Actually, I am not very familiar with Generics. It was not covered in my CS-1 course and my CS-2 professor has skipped it. From what I've read do I need to have a string and integer type?

What is the purpose of defining the type as ContactItem? Our professor provided us with a rough template of the assignment and this was the format of his program.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell: Thank you very much for that link, I'm still reading through the information, but I think this is going to be really helpful!
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I am not very familiar with Generics. It was not covered in my CS-1 course and my CS-2 professor has skipped it.


??? Well, okay, it's a CompSci class, not a Java class. But unless I'm misunderstanding, generics is the way to go.

If you say List<String>, you're saying that the List only contains strings. But String is just an object. Is there another object, one that contains a string and an integer, that you can use?
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is T? How can I tell what type is T?

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no “concrete” type in that code. You have a non‑concrete type, which is actually called a Formal Type Parameter, but no Actual Type Parameters. You may be able to create a List<Contact> however.

Are you really writing a class to implement the List<E> interface? That is probably beyond the scope of most class assignments. Are you sure you are not supposed to use a pre‑existing class?

Though I would not use a List to implement an address book myself.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I go about adding this contact object to an arraylist in another class?







 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why am I unable to add members to the arraylist of type Contact? It says I must: "The type ContactArrayList<E> must implement the inherited abstract method List<E>.add(E) " and so changed the type from Contact newMem to E newMem.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:Why am I unable to add members to the arraylist of type Contact? It says I must: "The type ContactArrayList<E> must implement the inherited abstract method List<E>.add(E) "

Actually, it says a bit more than that.

What is the signature of List.add(E)? And look at it carefully.

Winston

PS:
1. Whenever you override a method, ALWAYS add the '@Override' annotation.
2. Don't overload methods unless you have a very good reason - and getting rid of compiler errors is NOT a good reason.
At best you'll just mask something that is basically WRONG.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Actually, it says a bit more than that.
What is the signature of List.add(E)? And look at it carefully.


Given the add method in the List class is of the type E, it must remain the same type within the different class. How should I go about fixing that and be able to add the objects created in the Contact class to the arraylist?
Ty for your help.
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you take a look carefully at the signature of List.add(E) as Winston suggested?
What is the return type?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said before, are you really supposed to write your own List implementation? It has 24 methods which you have to implement correctly. That is going to take a long time.
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nah, 10 out of them are optional.

If you really, really must write a class for Contact list why not do something like this?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better to write a wrapper class. Give it the List as a private final member and delegate all method calls to it.
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Better to write a wrapper class. Give it the List as a private final member and delegate all method calls to it.

Campbell is right. Favour composition over inheritance.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, we are supposed to write our own List...



This is copied exactly from my professors template/sample code. From here we create an ArrayList class that will implement List and define each method in more detail. To be honest, I would have done this differently myself, but I thought it best to follow a similar outline. I'm very confused.
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least we know that you are not supposed to implement java.util.List but your professor's List.
And you are not supposed to "create" java.util.ArrayList but your professor's ArrayList.
Really poor and confusing choice of names.

If implementing this interface is a part of the assignment I guess you have no choice but to to it...

I would start by writing a class exactly as you said:
I would write dummy methods that throw UnsupportedOperationException (so it compiles).
Then I would write a constructor that initializes an empty list properly.
The I would implement all methods one-by-one (and I would test them).

This interface has an implicit expand() method. Does it mean that add or insert should throw ListIndexOutOfRangeException when there is no place for new element in array?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree it is a lot simpler to implement those nine methods. If you have been told to use that technique, you are stuck with having to use it.
I presume you know how to add elements sequentially to an array with size++ as the index.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel, thank you very much -- at least I will be heading in the right direction now. At the moment I have 4 classes (List, ArrayList, Contact, Main), I've used set/get methods to characterize each contact (name, phone, email..etc.). Each contact is able to have multiple addresses, phone numbers, emails -- how do I account for 'x' number of these? There must be a better way than creating multiple get/set() ( ex: getPhoneOne(), getPhoneTwo() ).

No, I do not understand size++ . This is all very new to me, most of this information was not covered in my CS-1 course.
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:Pawel, thank you very much -- at least I will be heading in the right direction now. At the moment I have 4 classes (List, ArrayList, Contact, Main), I've used set/get methods to characterize each contact (name, phone, email..etc.). Each contact is able to have multiple addresses, phone numbers, emails -- how do I account for 'x' number of these? There must be a better way than creating multiple get/set() ( ex: getPhoneOne(), getPhoneTwo() ).

If a contact can have any number of phones, then you should have a method that returns some kind of collection of phones.
Like public List<Phone> getPhones(). You can even use your List implementation .

Derek Nickerson wrote:No, I do not understand size++ . This is all very new to me, most of this information was not covered in my CS-1 course.

Basically, you keep size as a private field of your List implementation. Suppose you have a private array named array that you use for storing data.
You add an element by wriring Whis is an equivalent of writing

One more thing. I suppose you know that already but I'd say it anyway. Your ArrayList class should not know or care what Contact class is and what it does. It should just store any number of its instances.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean X number of persons infirmation?
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:Do you mean X number of persons infirmation?

I think Derek meant X number of phone numbers associated with one person.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
sai rama krishna wrote:Do you mean X number of persons infirmation?

I think Derek meant X number of phone numbers associated with one person.


Yes, Pawel is correct.
I'm going to look over and digest what you just wrote and see what I can do with that. Thanks a lot!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the simplest way to use size++. That adds 1 element at the end of the List. Adding in the middle is more complicated, because you have to move all subsequent elements up 1. So implement straightforward add first.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for toString: find out about the StringJoiner class in Java8. That will make toString much easier to implement, if you can use it.

I found out about StringJoiner at Devoxx
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote: . . .
Ty for your help.
You're welcome, but please spell it out in full; if you don't speak English and rely on an automatic translator, it may have difficulty with Ty.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My proposal to solve your problem would be:

1. Since contact is just a simple conceptual entity that contain fields of data and operations to set and get the data, create it first as an abstract class placing all the fields and operations in it as necessary... in that way you can [ extend ] / [ change ] the implementation in the case that requirements change...


2. Extend the abstract class to provide a concrete implementations for use within the application...



3. Create a skeleton implementation of the interface that you were given:


4. Use pencil and paper to determine algorithms (either diagram of pseudocode) that would accomplish each task of the individual methods...

5. Implement them in Java code and test it using a driver-class:


6. Write the GUI front-end and refine the collaborations between the classes...
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have one other question (for now), and I think I will be ready to roll. When I create a Contact object in the main class, there data type differences. Eclipse says: "The method setName(ContactItem) in the type Contact is not applicable for the arguments (String)". I thought that by defining our own type (ContactItem), that it would recognize any argument -- clearly not the case.

If I change the variables in the Contact class back to String type, how then do I change their type to ContactItem? In addition to that, how do I change their type to the generic to be stored in the ArrayList<E> ?




Main Class


Contact Class


*EDIT* Is it by using the toString() method?


*ContactItem class is intended for an XML parser which I have yet to create*
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:In addition to that, how do I change their type to the generic to be stored in the ArrayList<E> ?

It is the other way around.
You don't "make Contact generic" to fit into List<E>. You make List<E> generic to accept elements of type Contact (or any other type).

If you have a concrete implementation of List<E> named ArrayList<E> you can write:Since Java 7 you can skip redundand type declaration and write:E in a generic class is a placeholder that means "any type" *. You specify what this type is when you declare an instance of the class like in the example above.

You can also specify it in class declaration. You can write:*Note that it does not need to be named E. You can choose any legal identifier for it.

About ContactItem. If your method signature is:... then you can't pass a String to this method. You must pass an instance of ContactItem.
You need to convert the String to ContactItem first if your data comes from String. There are plenty ways to to this. You could write a constructor that accept a String. A static method that creates an instance... It's your call.

By the way. I don't like the idea of storing every contact information as an instance of ContactItem. I'd rather see PhoneNumber class for a phone number etc...
But maybe this would be an overkill for this assignment... ANd I am not sure why do you need ContactItem for XML... Never mind.
 
Paweł Baczyński
Bartender
Posts: 2075
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this in a new post so you don't miss it. It is very important.

Make only one thing at a time.

Focus on either:
  • writing an ArrayList<E> class that implements your professor's List<E> interface.
  • creating a Contact class.

  • Only when you finish doing one and test that it works correctly do the other.

    Your Contact class should be totally independent from your ArrayList<E> class and vice versa*.

    * Yes, I know that I said you can use ArrayList to store information like multiple phone numbers in your Contact. Forget about it for now.
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand now, that makes much more sense to me than what the text was saying about the generic type. Great help!

    By the way. I don't like the idea of storing every contact information as an instance of ContactItem. I'd rather see PhoneNumber class for a phone number etc...
    But maybe this would be an overkill for this assignment...


    How would it be beneficial to create a class for each item of information? How might you go about doing that? I see your point though, it is somewhat cumbersome instantiating each contact.
    From what I know, , classes are meant to define a broad type. For example, I would create a class for Book which can be subdivided within the class into different characteristics: genres..title...date..etc... but never create a class for Page. Probably a poor example but hopefully it gets my point across.

     
    Paweł Baczyński
    Bartender
    Posts: 2075
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:How would it be beneficial to create a class for each item of information?

    That is a point of Object Oriented Programming. You represent all things as objects.

    Derek Nickerson wrote:I would create a class for Book which can be subdivided within the class into different characteristics: genres..title...date..etc... but never create a class for Page.

    Why not? It is perfectly reasonable to have a class Book that has a list of Pages as a member.

    I guess that for this assignment you can just store everything as Strings.
    But remember. In general, StringsAreBad.
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is a point of Object Oriented Programming. You represent all things as objects.


    Ok, but isn't it better (more organized) to have all the information for a contact within a single class with parameters or to use setters/getters -- as opposed to having an object for each type of information?
    How should you determine whether to create a class or to do what I've done in this code by instantiating each contact?

    Forgive me for being a pain...but I really want to understand all of this.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!