配列のコピー(System.arraycopyとclone)
3593 ワード
package org.test.algo;
public class ArrCopyTest {
/**
* @param args
*/
public static void main(String[] args) {
// //
//
// // , a=b , ?
// String[] arr1=new String[]{"a","b","c"};
// ArrCopyTest.printArr(arr1);
// String[] arr1copy1=arr1;
// ArrCopyTest.printArr(arr1copy1);
// System.out.println("-------");
// arr1[1]="haha";// , arr1
// ArrCopyTest.printArr(arr1);
// ArrCopyTest.printArr(arr1copy1);// ,arr1 arr1copy1 ,
// // = ,
// //
// String[] arr1=new String[]{"a","b","c"};
// ArrCopyTest.printArr(arr1);
//
// //System.arraycopy(src, srcPos, dest, destPos, length);
// String[] arr1copy2=new String[arr1.length];
// System.arraycopy(arr1, 0, arr1copy2, 0, 3);
// ArrCopyTest.printArr(arr1copy2);
//
// System.out.println("arr1copy3-----arr1, 0, arr1copy3, 1, 1----------");
// String[] arr1copy3=new String[arr1.length];
// System.arraycopy(arr1, 0, arr1copy3, 1, 1);
// ArrCopyTest.printArr(arr1copy3);
//
// System.out.println("arr1copy4-----arr1, 1, arr1copy4, 1, 1----------");
// String[] arr1copy4=new String[arr1.length];
// System.arraycopy(arr1, 1, arr1copy4, 1, 1);
// ArrCopyTest.printArr(arr1copy4);
//
// System.out.println("arr1copy5-----arr1, 0, arr1copy5, 1, 2----------");
// String[] arr1copy5=new String[arr1.length];
// System.arraycopy(arr1, 0, arr1copy5, 0, 1);
// ArrCopyTest.printArr(arr1copy5);
//
// System.out.println("arr1copy6-----arr1, 0, arr1copy6, 1, 2----------");
// String[] arr1copy6=new String[arr1.length];
// System.arraycopy(arr1, 0, arr1copy6, 1, 2);
// ArrCopyTest.printArr(arr1copy6);
//
// String[] arr1=new String[]{"a","b","c"};
// ArrCopyTest.printArr(arr1);
//
// System.out.println("arr1copy7-----arr1, 0, arr1copy7, tergetPos, arr1copy7.length-tergetPos----------");
// String[] arr1copy7=new String[arr1.length];
// int tergetPos=0;// ,arr1copy7.length-tergetPos ,
// System.arraycopy(arr1, 0, arr1copy7, tergetPos, arr1copy7.length-tergetPos);
// ArrCopyTest.printArr(arr1copy7);
//
// arr1[1]="hello";// , arr1
// ArrCopyTest.printArr(arr1);
// ArrCopyTest.printArr(arr1copy7);
// clone()
String[] arr1=new String[]{"a","b","c"};
ArrCopyTest.printArr(arr1);
System.out.println("arr1copy8=arr1.clone()");
String[] arr1copy8=arr1.clone();
ArrCopyTest.printArr(arr1copy8);
arr1[1]="well";// , arr1
ArrCopyTest.printArr(arr1);
ArrCopyTest.printArr(arr1copy8);
}
public static void printArr(String arr[]){
if(arr==null){
System.out.println("arr is null");
}else{
System.out.println("arr.hashCode is:"+arr.hashCode());
for(int i=0;i