简体   繁体   中英

hashmap being re-used by new objects

I have an issue with a hashmap which is being reused by new objects and I can't seem to figure out how to give each new object its own hashmap. So essentially, when I generate a time table and I save it etc, when it comes to make the next time table, it uses the same roomList, ie the int[][] part of the hashmap for some rooms are already booked (which has been taken from the previous time table generated). What I want is an indiv object with a unique time table, unique list of rooms and the arr.

Below is my code:

The first is a class which generates a population of time tables.

public population(int size, boolean init, ArrayList<ListOfObj> arr, HashMap<Room, int[][]> roomList){
        listOfTables = new Tables[size];
        if(init){
            for(int i=0; i<listOfTables.length; i++){
                IndivTables indiv;
                CompleteTable[][][] table = new CompleteTable[5][9][];
                indiv = new IndivTables (arr,roomList,table);
                saveIndiv(i, indiv);
            }
        }
    }

The second is the actual class which creates the time tables.

    private CompleteTable[][][] timetable;
    private HashMap<Room, int[][]> roomList;
    private ArrayList<listOfObj> arr;

    public IndivTables (ArrayList<ListOfObj> arr, HashMap<Room, int[][]> roomList, CompleteTable[][][] table){
        this.arr = arr;
        table = generate(arr, roomList);
        this.timetable = table;
        this.roomList = roomList;
    }

Below is the function that creates the time table. This is in the same class as IndivTables.

    public static CompleteTable[][][] generate(ArrayList<ListOfObj> arr, HashMap<Room, int[][]> roomList){

        int rows = 5;
        int columns = 9;
        CompleteTable[][][] timeTable = new CompleteTable[rows][columns][];
        HashMap<Room, int[][]> roomListAll = new HashMap<Room, int[][]>(roomList);

        Random random = new Random();

        ListOfObj randomObj;

        Iterator<ListOfObj > iterator = arr.iterator();
        while(iterator.hasNext()){

            boolean clash = false;

            //Get random ListOfObj object
            randomObj= arr.get(random.nextInt(arr.size())); 

            //Allocate room based on most efficient - doesn't do anything to hashmap
            Room room = allocateRoom(roomListAll, randomObj, row, column);
            if(room != null){
                CompleteTable comp = new CompleteTable(randomObj, room);

                if(timeTable[row][column] != null && timeTable[row][column].length>0){
                    if(!clash){
                    int[][] val = roomListAll.get(room);
                    val[row][column] = 1;
                    roomListAll.put(room, val);
                }                   
             }else{                 
                int[][] val = roomListAll.get(room);
                val[row][column] = 1;
                roomListAll.put(room, val);
                clash = false;
             }

             if(!clash){
               arr.remove(randomObj);
             }
          } 
       }
    }
}
  }
 }      
   return timeTable;
 }

Thanks in advance for any help!!

You are using same hashmap reference to create new IndivTables in the for loop iteration. Hence it's using same HashMap.

Change the following line new IndivTables (arr,roomList,table) to new IndivTables (arr, new HashMap(), table);

However, if you want to retain the content of roomList, then do this new IndivTables (arr, new HashMap(roomList), table);

Please note, this is shallow copy and not deep copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM