I need to add an Object to an ordered ArrayList depending on an attribute inside of the Object. I know how to use the .add method to add the object but I don't know how to search for the right place for it using the compareTo() method. And also I need to remove an Object from the ArrayList if the Object contains a certain String but I cant figure out how to access the Object attributes from the ArrayList.
Realtor Object
/**
* Constructor for Realtor object using parameter
* @param readData - array of data from line
*/
public Realtor(String[]readData){
licenseNumber = readData[2];
firstName = readData[3];
lastName = readData[4];
phoneNumber = readData[5];
commission = Double.parseDouble(readData[6]);
}
RealtorLogImpl
public class RealtorLogImpl {
private ArrayList<Realtor> realtorList;
/**
* Add Realtor object to ordered list
* @param obj - Realtor object
*/
public void add(Realtor obj){
//needs to be added into correct place depending on Realtor licenseNumber
realtorList.add(obj);
}
/**
* Delete Realtor object from list if license matches
* and return true if successful
* @param license
* @return
*/
public boolean remove (String license){
//need to remove Realtor with specific licenseNumber and return true if successful
}
List<String> list = new ArrayList<>();andif (list.get(13).length() == 42) {...}Object, what does it mean for an element to "contain" a string?.equals()that has the "certain String"? If so, then the.remove(object)will work. Otherwise, you'll need to use anIterator, which will allow you to get the Object, and then use the standard methods to retrieve the value and compare. Second, do you really need an ArrayList (See: Why Is there No Sorted List). Best approach is just to add and then sort with an appropriate comparator.