This discussion is archived
3 Replies Latest reply: Mar 12, 2008 6:16 PM by EJP RSS

How to cast a List to an Object array/Suggestions to code more efficiently?

807591 Newbie
Currently Being Moderated
Hi All,

I am writing a block of code to remove elements from an Object array if a duplicate found and want a way to write it more efficiently. Currently, I am using 4 arrays:

usersAllPhases [] ---> array of users
reviewResult [] ---> another array to compare
newUsersAllPhasesNotYetAssigned [] = array to store new set of users
usersAllPhasesNotYetAssigned [] = arraylist to add new list to

Here is the code I've written:
User[] usersAllPhases = null;
User[] newUsersAllPhasesNotYetAssigned = null;

usersAllPhases = getUserArray();
ReviewResult[] reviewResult = (ReviewResult[]) request.getAttribute("RESULTS");

List usersAllPhasesNotYetAssigned = new ArrayList();
                
int userCount = 0;                                      
for (int i=0; i<usersAllPhases.length; i++){
    int usersAllPhasesId = usersAllPhases.getID();
for (int h=0; h<reviewResult.length; h++){
if (usersAllPhasesId != reviewResult[h].getReviewPerformer().getUserID()){
usersAllPhasesNotYetAssigned.add((User) usersAllPhases[i]);

}
}
}
for(int j=0; j<usersAllPhases.length; j++){
newUsersAllPhasesNotYetAssigned[j] = (User) usersAllPhasesNotYetAssigned.get(j);
}
As you can see, I'm looping through the first object array, then comparing one of it's properties to another array, then if a duplicate ID is not found, I'm adding the Object to an ArrayList.  I then save the ArrayList items to another User[] array (to be sent to a different method that requires a User[] Object array.

I know this can be done written more efficiently somehow.  Is there a better way to remove items from an Object array?  If not, is there a better way to cast a List to an Object array?

Thanks in Advance!
Blundt

Edited by: blundt on Mar 12, 2008 5:14 PM