Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Trouble with an array

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Trouble with an array

    Hey, im making a contact list for a uni project, and i want to add a contact type into my array list, but without using it in the Contact class in which this array list uses. Is there any way that i can add a contact type into this array list without putting it in the Contact class

    public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phonenumberEmail)
        {
            contactList.add(new Contact(fName, sName, street, town, partOnePC, partTwoPC, phonenumberEmail));
     
     
        }

    Thanks in advance.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Trouble with an array

    Hello 93tomh!
    Quote Originally Posted by 93tomh View Post
    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;
    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.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with an array

    Is this what you meant? (sorry im not great at java)

     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);
     
        }

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Trouble with an array

    What is contactType? Is it a String? If you define your contactList to store Contacts
    ie
    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
    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);
     
    }

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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)

    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?

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default 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.
    Last edited by veeer; July 25th, 2012 at 08:18 AM.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default 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.
    public final class Costumer {
     
      public static enum Type {
        PERSONAL, BUSINESS
      }
     
      private final Type type;
      ...
     
      public Contact(Type type, ...) {
        this.type = type;
        ...
      }
     
      ...
    }
    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.
    Last edited by veeer; July 25th, 2012 at 08:39 AM.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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?

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Trouble with an array

    Quote Originally Posted by 93tomh View Post
    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.

  11. #11
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Trouble with an array

    I believe what you may be looking for is a Map.
    enum ContactType {
      PERSONAL, BUSINESS
    }
    ...
    final Map<ContactType, Set<Contact>> contacts = new HashMap<>();
    ...
    public void addPersonalContact(...) {
      contacts.get(ContactType.PERSONAL).add(new Contact(...));
    }
    ...

Similar Threads

  1. Trouble Updating an Array
    By Gravity Games in forum Java Theory & Questions
    Replies: 36
    Last Post: July 25th, 2012, 12:52 AM
  2. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 07:41 PM
  3. Trouble passing array in parameters
    By AngryCrow in forum Collections and Generics
    Replies: 2
    Last Post: September 12th, 2011, 09:49 AM
  4. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  5. Grade Array Trouble
    By kite98765 in forum Collections and Generics
    Replies: 3
    Last Post: January 7th, 2010, 07:14 PM