-
Why Wont This Work???
Hi all, i have a programme of details of a business with various accessor methods and other methods i am currently stuck on how to search an array list to check if a client has the same address as another client i cant understand how i am going wrong please help?
Here is my basic code:
Code:
public MyClass sameAddress(MyClass value) {
MyClass result = null;
for (int i = 0; i < myArrayList.size(); i++) {
MyClass searchArray = (MyClass) myArrayList.get(i);
if (searchArray.getAddress().equals(value.getAddress()))
{
result = searchArray;
}
}
return result;
when i run this in the main method for example:
Code:
System.out.println(clientCollection.sameAddressCheck(client1));
It just returns the info for client 1?
The programme should look through the array list searchArray to find addresses until one matches value.getAddress() then return the address that matches client 1. Please Help!
-
Is the client1 object identical to the item it finds in the arraylist?
-
client1 is of an object of type MyClass which has methods such as getaddress getName etc I have managed to search the Arraylist in another method which finds a client in the array which is similar to this and it works here it is:
[CODE]
public String find (String name) {
String result = null;
for (int i = 0; i < myArrayList.size(); i++) {
MyClass searchArray = (MyClass) myArrayList.get(i);
if (searchArray.getFullName().getSurname().equals(las tName)) {
result = searchArray.getclient_id();
}
}
return result;
}
[\CODE]
this works perfectly and is done on i similar basis except in just returns the client id which is simply a field with an accessor method get client.
Cant see why this works but the other doesnt?
-
The arraylist is a list of MyClass objects? If it is it's probably returning the found object.
-
Yes it is. So how do i fix the problem i want it to return the object in the arrayList of type MyClass that has the same address as the object entered into the parenthesis in the main method eg. (client1)
-
It is returning it. The object it finds with the same address happens to be the same item.
Check the item you are searching for and the arraylist items manually, find the last one with a matching addresses.
-
How to i get it to return a matched address in the arraylist but not matching itself?
-
So you want to find an object that isn't the same as the input bit has the same address variable? You will have to overwrite equals and probably hashcode and add an additional test in the if clause.
-
The method should search MyClass for for any addresses that match in any of the objects in myarraylist by searching addresses in the Address class which is its own class with method getAddress()
this sort of outlines what i want not sure how to finish it off correctly:
Code:
public MyClass sameAddress(MyClass value) {
MyClass searchArray = (MyClass) myArrayList.get(i);
if (searchArray.getAddress().equals(value.getAddress()))
{
return ..... of type MyClass?
}
-
Create a toString method and print out the object it finds so you can see the object it returns.
-
Is this what you are trying to do?
Code:
public MyClass sameAddress(MyClass value) {
MyClass searchArray = (MyClass) myArrayList.get(i);
if (searchArray.getAddress().equals(value.getAddress()) &&
!searchArray.equals(value))
{
return searchArray
}
-
This is what I was thinking you were asking as well but I couldn't provide the code on my phone.