I have this code:
public class Server{
public static ArrayList<Server> all;
public int id;
public int sid;
public Server(int id){
thid.id = id;
sid = fetchSidFromDB(id);
}
public Server(int id, int sid){
thid.id = id;
sid = sid;
}
public static void loadAll(){
//Assume that I fill 'all' with the servers from the db
all = new ArrayList<Server>();
}
//gets only a list of 'id's with out the sid
public static void refreshAll(){
}
}
//in the main
Server.loadAll();
Server.refreshAll();
I want that refreshAll will get new list from the db and do:
- If the
idisnt in the object already - insert it - If the
idis in the object but the sid isnt the same - replace it - If there is an
idinallthat isnt in the new list - remove it
This is simple, but as I see it I can only do it with:
for(...){
for(...){
}
for(...){
}
One inside for for step 1&2 and one inside for for step 3.
I wonder if there's more efficient way.
sid = fetchSidFromDB(id);is just an example, it is much more complicated then that, that is what i'm try to avoid from.