//Back Tracking에서 deep copy가 상당히 중요한데

public static List<Fish> deepCopyArrayList(List<Fish> arrayList) {
	
	List<Fish> copyList = new ArrayList<Fish>();
	for(Fish fish: arrayList) {
		copyList.add(fish);
	}
	return copyList;
}

//위와 같이 처음에 코드를 기입했었는데, 문제가 뭘까?
//List자료구조만 deep Copy해놓고 그 안에 들어갈 객체들을 싹다 그냥 그대로 넣어버렸다. 쉘로우 카피도 아님
//그냥 넣게 됨

https://www.techiedelight.com/ko/copy-objects-in-java/ → 딥 카피

class Fish implements Cloneable{
	
	int id, dir, x, y;
	boolean alive;

	public Fish(int id, int dir, int x, int y, boolean alive) {
		this.id = id;
		this.dir = dir;
		this.x = x;
		this.y = y;
		this.alive = alive;
	}
	
	//복사를 위한 생성자
	public Fish(Fish targetFish) {
		this.id = targetFish.id;
		this.dir = targetFish.dir;
		this.x = targetFish.x;
		this.y = targetFish.y;
		this.alive = targetFish.alive;
	}

	@Override
	public String toString() {
		return "Fish [id=" + id + ", dir=" + dir + ", x=" + x + ", y=" + y + ", alive=" + alive + "]";
	}

}

위와같이 복사를 위한 생성자를 만들어 놓는다. 만약 객체 안에 List등의 자료구조가 있다면 new를 해서 새로 생성해주고 넣어줘야 한다.

public static List<Fish> deepCopyArrayList(List<Fish> arrayList) {
		
		List<Fish> copyList = new ArrayList<Fish>();
		for(Fish fish: arrayList) {
			Fish copiedFish = new Fish(fish); //이 부분때문에 상당히 오래걸렸다 딥카피가 안돼서.. 복사 생성자 방법이 있네
			copyList.add(copiedFish);
		}
		return copyList;
	}

객체 생성자를 이용해서 new로 새로운 객체를 생성하고 내용물을 복사해서 넣어준다. 그 안에 stack memory에 영향을 주는 primitive variable들을 넣어서 새로 생성해준다.