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 20 of 20

Thread: finding and deleting from an array?

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

    Default finding and deleting from an array?

    Ok, so im struggling to find a 'contact' and also deleting a 'contact' in my array list. Can anyone tell me how i can do this? Thanks


    import java.util.ArrayList;
     
    public class ContactList
    {
        private ArrayList<Contact> contactList;
     
        private String contactType;
     
        public ContactList()
        {
     
            contactList = new ArrayList<Contact>();
     
        }
        public void addPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phoneNumber)
        {
            contactList.add(new AddPersonalContact(fName, sName, street, town, partOnePC, partTwoPC, phoneNumber));
     
        }
     
        public void addBusinessContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String eMail)
        {
     
            contactList.add(new AddBusinessContact(fName, sName, street, town, partOnePC, partTwoPC, eMail));
     
     
        }
     
        public String toString() {
            final StringBuilder output = new StringBuilder("All Contacts:\n");
            for (Contact contact : contactList) {
                output.append(contact).append("\n\n");
            }
            return output.toString();
        }
     
        public String getPersonalContacts()
        {
            String output = "All Personal Contacts:\n";
            for (Contact contact : contactList)
            {
                if(contact instanceof AddPersonalContact)
                {
                    output = output + contact + "\n\n" ;
                }
            }
     
            return output;
        }
     
        public String getBusinessContacts()
        {
            String output = "All Business Contacts:\n";
            for (Contact contact : contactList)
            {
                if(contact instanceof AddBusinessContact)
                {
                    output = output + contact + "\n\n" ;
                }
            }
     
            return output;
        }
     
    }
    Last edited by 93tomh; August 12th, 2012 at 06:02 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    Where in the posted code are you having the problem?

    If you haven't written any code yet, can you list your ideas on how to implement the code you want to write as a list of simple steps(pseudocode) that will solve the problem.
    Last edited by Norm; August 12th, 2012 at 07:22 AM.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    I want to search by first and last name, im guessing for finding a contact;

    1. returning the full array list and then sorting the results by name
    or
    2. Searching through the array list only for the first and last name

    and as for deleting, I want to say find the integer value of the part of the array list the contact is in and delete it, but I am not sure how to do it

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    What problems are you having writing the code? Take the items in the list one at a time:
    write a method that returns the arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    I know that
    public String toString() {
            final StringBuilder output = new StringBuilder("All Contacts:\n");
            for (Contact contact : contactList) {
                output.append(contact).append("\n\n");
            }
            return output.toString();
        }

    returns the array list, but i have no idea how to then sort it

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    No, the toString() method returns a String. It does NOT return an ArrayList.
    Look at the tutorial on how to define methods:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    The posted code does not compile without errors. Several of the missing class definitions have names that suggest you think a method is a class. The names of classes should be nouns. The names of methods should be verbs. AddPersonalContact is not a good class name. Better would be PersonalContact.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    sorry i havent replied, i tried for a couple of hours yesterday to get my head around this and got nowhere. But digging up some of my old assignments I found this piece of code, would what i am trying to accomplish look anything like this?

     private int findAccount(String accountNumber)
        {
            int index = 0;
            boolean found = false;
            while(index < accounts.size() && !found)
            {
                BankAccount account = accounts.get(index);
                if(account.getAccountNumber().equals(accountNumber))
                {
                    found = true;
                }
                else
                {
                    index++;
                }
            }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    would what i am trying to accomplish look anything like this?
    That code shouldn't compile. The method is defined to return an int value but it doesn't return anything.

    You should fix the compiler errors in the code you posted in #1 before trying to add more code to it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    Would it not compile because i didnt give you the other 3 classes that are used? Because it compiles at my end perfectly.

     
     
    public abstract class Contact
    {
        protected String fName;
        protected String sName;
        protected String street;
        protected String town;
        protected String partOnePC;
        protected String partTwoPC;
        protected String postcode;
     
     
        public Contact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC)
        {
     
           this.fName = fName;
            this.sName = sName;
            this.street = street;
            this.town = town;
     
     
     
     
     
     
     
            if(partOnePC != null && (partOnePC.length() >= 3 && partOnePC.length() <= 4))
            {
                this.partOnePC = partOnePC;
     
     
            }
            else
            {
              System.out.println("Please insert a string with 3-4 characters for part one postcode");  
            }
     
            this.partOnePC = partOnePC;
            if(partTwoPC != null && (partTwoPC.length() >= 3 && partTwoPC.length() <=4))
            {
               this.partTwoPC = partTwoPC;
            }
            else
            {
                System.out.println("Please insert a string with 3-4 characters for part two postcode");
            }
     
            this.postcode = partOnePC + " " + partTwoPC; 
        }
     
            public String toString()
            {
                String output = "First Name: " + fName + "\n" + "Second Name: " + sName + "\n" + "Street " + street + "\n" + "Town: " + town + "\n" + "Postcode: " + postcode;
     
                return output;
     
            }
     
     
     
     
     
    }

     
    public class AddPersonalContact extends Contact
    {
     
        public String contactType;
        public String phoneNumber;
     
        public AddPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phoneNumber)
        {
           super(fName, sName, street, town, partOnePC, partTwoPC);
     
           this.phoneNumber = phoneNumber;
           this.contactType = "Personal";
        }
     
     
        public String toString()
        {
     
            String output = super.toString() + "\n" + "Phonenumber: " +  phoneNumber + "\n" + "Contact Type: " + contactType;
            return output;
     
        }
     
    }

     
     
     
    public class AddBusinessContact extends Contact
    {
       public String contactType;
       public String eMail;
     
        public AddBusinessContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String eMail)
        {
           super(fName, sName, street, town, partOnePC, partTwoPC);
           this.eMail = eMail;
           this.contactType = "Business";
        }
     
             public String toString()
        {
     
            String output = super.toString() + "\n" + "E-Mail: " +  eMail + "\n" + "Contact Type: " + contactType;
            return output;
     
        }
     
     
     
    }

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

    Default Re: finding and deleting from an array?

    and sorry, missed the end bit ... silly me

    private int findAccount(String accountNumber)
        {
            int index = 0;
            boolean found = false;
            while(index < accounts.size() && !found)
            {
                BankAccount account = accounts.get(index);
                if(account.getAccountNumber().equals(accountNumber))
                {
                    found = true;
                }
                else
                {
                    index++;
                }
            }
            if (found)
                return index;
            else
                return -1;
        }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    Your class names don't follow normal naming conventions. Classes are named for things. Methods are named for the actions they do.
    AddPersonalContact sounds like a method name. A better class name would be PersonalContact.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    I was always taught in uni to use things like 'addPersonalContact' for a method, and its what i am used to. I have reposted the account code, which i missed off the last bit by accident first time. Can you tell me if this is a potential solution for my problem? thanks

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    Please say which part of the problem you are working on. I suggested in posts #2 and #4 that you make a list of the steps the program must take and then do them one at a time.
    One of the steps was to add a method that returns the array list. The method posted in post#10 returns an int not the array list, so I don't see how it would help.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    Im trying to work on the finding contact first. Also I dont know how to return an array list, ive been trying to understand it forages now but cant get my head around it. What annoys me about this is that i dont understand most of it and it isn't even relevant in my course but they make you do it in the first year anyway! I'm really sorry but it really is hard for me to understand.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    Ok, work on finding a Contact. Here are things to consider:
    Will the search be in a method? What will the method return?
    When you look at a Contact object, how do you determine it is the one you are looking for?
    You'll need a loop to look at each object and test it.
    When a match is found, return ???
    If no match found, return ???
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    Yes, it will be in a method of its own, and im hoping to return the name address and all the details of that entity. Am i going to be trying to say "if firstName == fName && secondName == sName" but then i am not sure how to go through the array list doing this.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    Several new questions:
    What argument(s) will be passed to the method?
    What will the method return?

    how to go through the array list doing this.
    The for loop is a natural for going through arraylists.
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: finding and deleting from an array?

    so now i have

    public int findContactIndex(String firstName, String secondName)
        {
            int index = 0;
            boolean found = false;
            while(index<contactList.size() && !found)
            {	
                Contact contacts = contactList.get(index);
                if(contacts.getFirstName().equals(firstName) && contacts.getSecondName().equals(secondName))
                {
                    found = true;
     
                }
                else
                {
                    index++;
                }
            }
            if(found)
                return index;
            else
                return -1;
        }

    this returns the index of an entity based on a full name search. Can i then, in another method say:

    return the array list at index (x)

    x being the index found in the previous piece of code

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: finding and deleting from an array?

    Did you test it? Did it work?

    Why have the boolean variable? Why not return index directly?

    return the array list at index (x)
    Read the API doc for the ArrayList class to see what method(s) to use.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    93tomh (August 13th, 2012)

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

    Default Re: finding and deleting from an array?

    I GOT IT!

    Thank you very much for your help and suggestions norm, it is very appreciated!

Similar Threads

  1. Deleting Array Records
    By rainexel in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 11:40 AM
  2. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  3. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM
  4. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM
  5. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM