Re: Trouble with an array
Hello 93tomh!
Quote:
Originally Posted by
93tomh
Is there any way that i can add a contact type into this array list without putting it in the Contact class
Yes, you can do that if the other class has access to the Contact class (if they are in the same folder). Example;
Code :
class A { //like your Contact
String aString;
String bString;
public A(String aString, String bString) {
this.aString = aString;
this.bString = bString;
}
}
class B {
ArrayList<A> aList = new ArrayList<A>();
public void addA(String a, String b) {
aList.add(new A(a, b));
}
}
Hope this helps.
Re: Trouble with an array
Is this what you meant? (sorry im not great at java)
Code :
public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phonenumberEmail)
{
contactType = "Personal";
contactList.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC, phonenumberEmail));
contactList.add(contactType);
}
Re: Trouble with an array
What is contactType? Is it a String? If you define your contactList to store Contacts
ie
Code :
ArrayList<Contact> contactList = new ArrayList<Contact>();
then it can only store Contacts.
In the addPersonalContact(..) method you can create a new Contact with the parameters you pass to the method and then store it in the contactList.
ie
Code :
public void addPersonalContact(String fName, String sName, String street, String partOnePC, String partTwoPC, int phoneNumber) {
Contact aContact = new Contact(fName, sName, street, town, partOnePC, partTwoPC);
contactList.add(aContact);
}
Re: Trouble with an array
hi, I have tried this (or at least what i think youre trying to tell me) and i am getting a compiling error.
No suitable method found for add(java.util.ArrayList<Contact>)
method java.util.ArrayList.add(int, Contact) is not applicable
(actual and formal argument lists differ in length)
method java.util.ArrayList.add(Contact) is not applicable
(actual argument java.util.ArrayList<Contact> cannot be converted to Contact by method invocation conversion)
Code :
public class ContactList
{
private ArrayList<Contact> contactList;
private ArrayList<Contact> aContact;
private String contactType;
public ContactList()
{
contactList = new ArrayList<>();
aContact = new ArrayList<Contact>();
ArrayList<Contact> contactList = new ArrayList<Contact>();
}
public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phonenumberEmail)
{
contactType = "Personal";
aContact.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC, phonenumberEmail));
contactList.add(aContact);
}
Also would it be an alternative to have multiple pieces of code in Contact or does an Array List only takeinputs from the constructor?
Re: Trouble with an array
Why should the contact type *not* be apart of the Contact object? There's no real logical alternative... you should be seeking to maximize cohesion while minimizing coupling.
Re: Trouble with an array
There are two contact types, business and personal, and i want them to be put into the array list without the person actually typing it in themselves. But i am not sure how to use a different method to the constructor when creating an array list
Re: Trouble with an array
I see no reason why it shouldn't be exposed to the user, but if you must, just add a constructor parameter and seek adding a type field to the Contact class and add it to the class' constructor. That way you can specify types at creation time. I'd probably advise an enum in this case, too... see something like the below code.
Code java:
public final class Costumer {
public static enum Type {
PERSONAL, BUSINESS
}
private final Type type;
...
public Contact(Type type, ...) {
this.type = type;
...
}
...
}
Code java:
public final class ContactList {
...
public void addPersonalContact(...) {
contactList.add(new Contact(Contact.Type.PERSONAL, ...));
}
...
}
I recognize you requested a way to avoid modifying the Contact class, but I fail to see any rational reason in doing so. Again, you should be seeking high cohesion, low coupling.
Re: Trouble with an array
Im not too comfortable with using this as i have not used it before as this is also a uni project. If i have several methods in Contact (as apposed to using the constructor) can i use the individual methods in Contact rather than the constructor?
Re: Trouble with an array
Quote:
Originally Posted by
93tomh
Im not too comfortable with using this as i have not used it before as this is also a uni project. If i have several methods in Contact (as apposed to using the constructor) can i use the individual methods in Contact rather than the constructor?
What kinds of individual methods? Accessors and mutators for the type? Sure, but I see no benefit over using the constructor to pass your type in.
Re: Trouble with an array
Instead of using the constructor, I was thinking of using
public void createPersonal(orBusiness)Class()
But i do not know how to call this in the array list
Re: Trouble with an array
I believe what you may be looking for is a Map.
Code java:
enum ContactType {
PERSONAL, BUSINESS
}
...
final Map<ContactType, Set<Contact>> contacts = new HashMap<>();
...
public void addPersonalContact(...) {
contacts.get(ContactType.PERSONAL).add(new Contact(...));
}
...