java四種拷貝方式及常見的陣列操作方法
阿新 • • 發佈:2018-11-02
一、四種拷貝方式
要確定一個方法是淺拷貝還是深拷貝,必須用引用型別的值,所以要用物件定義陣列,以下四種方法的程式碼中都用物件定義了陣列以確定拷貝型別
1.for迴圈拷貝
class TestArray {
private int val = 10;
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return this.val;
}
}
public class TestDemo {
public static void main3(String[] args) {
TestArray[] t1 = new TestArray[4];
t1[0] = new TestArray();
t1[1] = new TestArray();
t1[2] = new TestArray();
t1[3] = new TestArray();
TestArray[] t2 = new TestArray[4];//t2[0]
for(int i = 0;i < t1.length;i++) {
t2[ i] = t1[i];
}
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
System.out.println();
t2[ 0].setVal(100000);
System.out.println("===============");
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
}
}
輸出結果:
由輸出結果可以看出在改變陣列第一個元素的情況下,由物件定義的陣列中元素也改變了,所以for迴圈的拷貝方式為淺拷貝
2.clone()方法
class TestArray {
private int val = 10;
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return this.val;
}
}
public class TestDemo {
public static void main(String[] args) {
TestArray[] t1 = new TestArray[4];
t1[0] = new TestArray();
t1[1] = new TestArray();
t1[2] = new TestArray();
t1[3] = new TestArray();
TestArray[] t2 = t1.clone();
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
System.out.println();
t2[0].setVal(100000);
System.out.println("===============");
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
}
}
由輸出結果可以看出在改變陣列第一個元素的情況下,由物件定義的陣列中元素也改變了,所以clone()的拷貝方式為淺拷貝
此方法會產生新的物件
3.System.arraycopy方法
此方法是淺拷貝,不會產生新的物件
class TestArray {
private int val = 10;
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return this.val;
}
}
public class TestDemo {
public static void main11(String[] args) {
TestArray[] t1 = new TestArray[4];
t1[0] = new TestArray();
t1[1] = new TestArray();
t1[2] = new TestArray();
t1[3] = new TestArray();
TestArray[] t2 = new TestArray[4];
System.arraycopy(t1,0,t2,0,t1.length);
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
System.out.println();
t2[0].setVal(100000);
System.out.println("===============");
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
}
}
//System.arraycopy()方法java原始碼中的程式碼
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
native :呼叫的是本地方法,此方法執行速度非常快
src :源地址
srcPos:源地址開始位置
dest:目的地
destPos:目的地的開始位置
length:拷貝的長度
4.Arrays.copyOf()方法
此方法為淺拷貝,此方法底層呼叫的還是System.arraycopy()方法。
此方法會產生新的物件
class TestArray {
private int val = 10;
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return this.val;
}
}
public class TestDemo {
public static void main(String[] args) {
TestArray[] t1 = new TestArray[4];
t1[0] = new TestArray();
t1[1] = new TestArray();
t1[2] = new TestArray();
t1[3] = new TestArray();
TestArray[] t2 = Arrays.copyOf(t1,t1.length);
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
System.out.println();
t2[0].setVal(100000);
System.out.println("===============");
for(int i = 0;i < t1.length;i++) {
System.out.print(t1[i].getVal()+" ");
}
System.out.println();
for(int i = 0;i < t2.length;i++) {
System.out.print(t2[i].getVal()+" ");
}
}
}
此方法使用時共有兩個引數,第一個為源陣列,第二個為要拷貝的陣列長度
二、常用陣列操作方法
1.如何填充陣列(一次填充,部分填充)
public class TestDemo3 {
public static void main(String[] args) {
int[] array = new int[10];
Arrays.fill(array, 15);
System.out.println(Arrays.toString(array));
int[] brray = new int[10];
Arrays.fill(brray, 0, 5, 15);
System.out.println(Arrays.toString(brray));
}
}
結果如下:
2.搜尋陣列中的最小值和最大元素
將一個數組放入stream中,直接呼叫stream中的min和max方法求值
public class TestDemo3 {
public static void main(String[] args) {
int[] array = {1,5,8,2,1,5,6};
int min = Arrays.stream(array).min().getAsInt();
System.out.println(min);
int max = Arrays.stream(array).max().getAsInt();
System.out.println(max);
}
}
3.如何合併兩個陣列(合併到一個新的陣列)
public class TestDemo3 {
public static void main(String[] args) {
int[] array1 = {1,2,3,4,5};
int[] array2 = {6,7,8,9,10};
int[] array3 = new int[array1.length+array2.length];
System.arraycopy(array1,0,array3,0,array1.length);
System.arraycopy(array2,0,array3,array1.length,array2.length);
System.out.println(Arrays.toString(array3));
}
}
4.如何從陣列中查詢常見的元素
public class TestDemo3 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7};
System.out.println(Arrays.binarySearch(array,4));
}
}
//找到返回key的下標,沒找到返回-1
5.如何刪除陣列指定元素
public class TestDemo3 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7};
for (int i = 0; i < array.length; i++) {
System.out.println(i + "號下標" + "指向數字: " + array[i]);
}
Scanner in = new Scanner(System.in);
System.out.println("輸入要刪除的數的下標:");
int del = in.nextInt();
for (int i = del; i < array.length - 1; i++) {
array[i] = array[i + 1];//將要刪除的下標後的值全部前移一位,即覆蓋所要刪除的數
}
int[] brray = Arrays.copyOf(array, array.length - 1);
System.out.println(Arrays.toString(brray));
}
}
6.如何排序陣列並插入某個元素
public class TestDemo3 {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7};
Arrays.sort