Copy Algorithm in java