陣列複製
發表於 : 2014年 8月 1日, 11:40
int[] score1 = {...};
int[] score2 = new int[score1.length];
Original:
for (int i=0; i < score1.length; i++) {
score2 = score1;
}
System.arraycopy(...):
System.arraycopy(score1, 0, score2, 0, score1.length);//0: init index
Arrays.copyOf() for after JDK6:
int[] score2 = Arrays.copyOf(score1, score1.length);
or
int[] score2 = Arrays.copyOf(score1, score1.length *2);//all the value of tail half are 0
int[] score2 = new int[score1.length];
Original:
for (int i=0; i < score1.length; i++) {
score2 = score1;
}
System.arraycopy(...):
System.arraycopy(score1, 0, score2, 0, score1.length);//0: init index
Arrays.copyOf() for after JDK6:
int[] score2 = Arrays.copyOf(score1, score1.length);
or
int[] score2 = Arrays.copyOf(score1, score1.length *2);//all the value of tail half are 0