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
Code :
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;
}
}
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.
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
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.
Re: finding and deleting from an array?
I know that
Code :
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
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.
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?
Code :
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++;
}
}
Re: finding and deleting from an array?
Quote:
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.
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.
Code :
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;
}
}
Code :
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;
}
}
Code :
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;
}
}
Re: finding and deleting from an array?
and sorry, missed the end bit ... silly me
Code :
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;
}
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.
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
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.
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.
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 ???
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.
Re: finding and deleting from an array?
Several new questions:
What argument(s) will be passed to the method?
What will the method return?
Quote:
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)
Re: finding and deleting from an array?
so now i have
Code :
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
Re: finding and deleting from an array?
Did you test it? Did it work?
Why have the boolean variable? Why not return index directly?
Quote:
return the array list at index (x)
Read the API doc for the ArrayList class to see what method(s) to use.
Re: finding and deleting from an array?
I GOT IT! :D
Thank you very much for your help and suggestions norm, it is very appreciated!