/** * Changes the dimensions of the grid to be the same as the one provided, then sets all the * locations in the grid to the elements at the quivalent locations in the provided grid. */ public final IntGrid3D setTo(IntGrid3D values) { if (width != values.width || height != values.height || length != values.length) { final int width = this.width = values.width; final int height = this.height = values.height; /*final int length = */ this.length = values.length; field = new int[width][height][]; int[][] fieldx = null; int[][] ofieldx = null; for (int x = 0; x < width; x++) { fieldx = field[x]; ofieldx = values.field[x]; for (int y = 0; y < height; y++) fieldx[y] = (int[]) (ofieldx[y].clone()); } } else { int[][] fieldx = null; int[][] ofieldx = null; for (int x = 0; x < width; x++) { fieldx = field[x]; ofieldx = values.field[x]; for (int y = 0; y < height; y++) System.arraycopy(ofieldx[y], 0, fieldx[y], 0, length); } } return this; }
/** * Assign the set of students (presumably non-incoming-freshmen, but this is not checked) passed * to their dorm rooms, in a way that does not take race into account. (compare {@link * #assignByRace}.) As a precursor (side effect) to this, any students already existing in * upperclass dorms will be removed. */ public void assign(Bag students) { emptyDorms(); // Purge all freshmen. (We're not assigning those.) Bag upperclassmen = null; try { upperclassmen = (Bag) students.clone(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } for (int x = upperclassmen.size() - 1; x >= 0; x--) { Student s = (Student) upperclassmen.get(x); if (s.getGrade() < 2) { upperclassmen.remove(s); } } for (int x = 0; x < upperclassmen.size(); x++) { Student s = (Student) upperclassmen.get(x); if (s.getGrade() < 2) { // We're only assigning upperclassmen here. continue; } s.leaveRoom(); for (int y = 0; y < dorms.size(); y++) { if (s.hasRoom()) { break; } Dorm d = (Dorm) dorms.get(y); if (d.isFull()) { continue; } if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) { continue; } int i = 0; while (i < d.getNumRooms()) { if (s.hasRoom()) { break; } try { d.getRoomByIndex(i).addResident(s); } catch (IllegalArgumentException e) { // System.out.println(e); } i++; } } } // Error testing to see if there are upperclassmen who don't have a // room/dorms that aren't full // int q=0; for (int x = 0; x < upperclassmen.size(); x++) { Student s = (Student) upperclassmen.get(x); if (!s.hasRoom()) { System.out.println(s + " has no upperclass dorm room!"); } } }
public static void main(String[] args) { doLoop(Schelling.class, args); System.exit(0); }