数组的赋值 杨辉三角 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package com.xiebin.day4;public class ArrayExer { public static void main (String[] args) { int [][] yangHui = new int [10 ][]; for (int i = 0 ; i <yangHui.length ; i++) { yangHui[i] = new int [i+1 ]; yangHui[i][0 ] = 1 ; yangHui[i][i] = 1 ; if (i > 1 ){ for (int j = 1 ; j < yangHui[i].length - 1 ; j++) { yangHui[i][j] = yangHui[i -1 ][j -1 ] +yangHui[i - 1 ][j]; } } } for (int i = 0 ; i < yangHui.length; i++) { for (int j = 0 ; j < yangHui[i].length; j++) { System.out.print(yangHui[i][j] + "\t" ); } System.out.println(); } } } ================================================= 输出结果: 连接到目标 VM,地址:'127.0.0.1:59938' , transport: 'socket' 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 断开与目标 VM 的连接,地址:'127.0.0.1:59938' , transport: 'socket' 进程已结束,退出代码 0
数组的复制 数组的复制:
1.for循环复制;
2.clone复制;
3.System.arraycopy复制;
4.Arrays.copyof复制
for循环复制 数组属于引用数据类型,变量保存的是地址值.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.xiebin.day4;public class ArrayCopy1 { public static void main (String[] args) { int [] arr,arr2; arr = new int []{1 ,2 ,3 ,4 ,5 ,6 }; for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "\t" ); } arr2 = arr; for (int i = 0 ; i < arr2.length; i++) { if (i % 2 == 0 ){ arr2[i] = i; } } System.out.println(); for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "\t" ); } System.out.println(); System.out.println("arr = " + arr); System.out.println("arr2 = " + arr2); } } ============================ 1 2 3 4 5 6 0 2 2 4 4 6 arr = [I@7 a7b0070 arr2 = [I@7 a7b0070
通过上述代码我们可以知道, arr2 = arr;这一步只是把arr保存的地址值[I@7a7b0070赋值给arr2,所以arr和arr 保存的是同一个地址值,修改arr2元素的值,arr元素的值也会随着改变.我们通过画图看看其怎么变化的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.xiebin.day4;public class ArrayCopy { public static void main (String[] args) { int [] arr = new int []{1 ,2 ,3 ,4 ,5 ,6 }; int [] arr2 = new int [arr.length]; for (int i = 0 ; i < arr2.length; i++) { arr2[i] = arr[i]; System.out.print(arr2[i]); } System.out.println(); arr[0 ] = 100 ; for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]); } System.out.println(); System.out.println("arr2 = " + arr2); System.out.println("arr = " + arr); } } ================== 123456 123456 arr2 = [I@1 b6d3586 arr = [I@4554617 c
通过for循环把arr复制给arr2,然后修改arr数组第一个元素的值发现arr2数组对应位置的值没有变,说明两个变量指向的不是同一块地址.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.xiebin.day4;public class Arraycopy2 { public static void main (String[] args) { String[] arr = new String[]{"AA" ,"BB" ,"CC" ,"DD" ,"EE" ,"FF" }; String[] arr2 = new String[arr.length]; for (int i = 0 ; i < arr2.length; i++) { arr2[i] = arr[i]; System.out.print("arr2数组元素" +arr2[i]+"\t" ); } System.out.println(); for (int i = 0 ; i < arr.length; i++) { System.out.print("arr = " + arr[i]+"\t" ); } System.out.println(); arr[0 ] = "aa" ; for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]+"\t" ); } } }
clone复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package com.xiebin.day4;public class ArrayClone { public static void main (String[] args) { int [] arr = {1 ,2 ,3 ,4 ,5 }; int [] arr2 = new int [arr.length]; arr2 = arr.clone(); for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+"\t" ); } System.out.println(); for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]+"\t" ); } System.out.println(); System.out.println("==========================" ); arr[0 ] = 10 ; for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+"\t" ); } System.out.println(); for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]+"\t" ); } System.out.println(); System.out.println("arr2 = " + arr2); System.out.println("arr = " + arr); } } ------------------------------------------------------------ 1 2 3 4 5 1 2 3 4 5 ========================== 10 2 3 4 5 1 2 3 4 5 arr2 = [I@1 b6d3586 arr = [I@4554617 c
通过上述代码的演示,我们发现,通过clone方法进行数组的复制,通过改变数组arr元素的值,然后arr2对应位置元素的值却没有发生改变,说明两数组变量保存的不是同一个地址值.我们打印两个数组的地址值发现,地址值不同.
总结:
一维数组:深克隆;(重新分配空间,并将元素复制过去) 二维数组:浅克隆。(只传递引用) 分析:这里采用"数组降维度"的思想,一维数组降维度就成为元素,基本数据类型;而二维数组降维度就为一维数组,数组是引用数据类型,复制的是引用.
浅复制和深复制
浅拷贝只复制指向某个对象的指针,而不复制对象本身,新旧对象还是共享同一块内存。但深拷贝会另外创造一个一模一样的对象,新对象跟原对象不共享内存,修改新对象不会改到原对象 ;
深拷贝指的是对象A复制给对象B时,A和B指向的不是同一个对象,通时A的对象成员AA和B的对象成员BB也指的不是同一个对象,如有更深层对象,以此类推。
浅克隆中,当对象被复制时只复制它本身和其中包含的值类型的成员变量,而引用类型的成员对象并没有复制
在Java语言中,通过覆盖Object类的clone()方法可以实现浅克隆 。
在深克隆中,除了对象本身被复制外,对象所包含的所有成员变量也将复制。
在Java语言中,如果需要**实现深克隆,可以通过*覆盖Object类的clone()方法实现,也*可以通过序列化(Serialization)等方式 来实现。
Java语言提供的Cloneable接口和Serializable接口的代码非常简单,它们都是空接口,这种空接口也称为标识接口,标识接口中没有任何方法的定义,其作用是告诉JRE这些接口的实现类是否具有某个功能,如是否支持克隆、是否支持序列化等。
实现对象克隆有两种方式:
1). 实现Cloneable接口并重写Object类中的clone()方法;
2). 实现Serializable接口,通过对象的序列化和反序列化实现克隆,可以实现真正的深度克隆。
System.arraycopy() 1 2 3 4 5 6 7 8 9 10 11 12 13 System.arraycopy()源码 * @param src 原数组. * @param srcPos 从原数组的那个位置开始拷贝. * @param dest 目标数组. * @param destPos 拷贝到目标数组的位置. * @param length 拷贝数组的长度. * @exception IndexOutOfBoundsException 索引越界异常. * @exception ArrayStoreException . * @exception NullPointerException 空指针异常. */ public static native void arraycopy (Object src, int srcPos, Object dest, int destPos, int length) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.xiebin.day4;public class ArrayCopy3 { public static void main (String[] args) { int [] arr = new int []{1 ,2 ,3 ,4 ,5 }; int [] arr2 = new int [arr.length]; System.arraycopy(arr,0 ,arr2,0 ,arr.length); System.out.println("-------------------------------------" ); arr[0 ] = 100 ; for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+"\t" ); } System.out.println(); for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]+"\t" ); } System.out.println(); System.out.println("arr2 = " + arr2); System.out.println("arr = " + arr); } } ========================= ------------------------------------- 100 2 3 4 5 1 2 3 4 5 arr2 = [I@1 b6d3586 arr = [I@4554617 c
Arrays.copyof() 首先看下它的源码
1 2 3 4 5 6 public static int [] copyOf(int [] original, int newLength) { int [] copy = new int [newLength]; System.arraycopy(original, 0 , copy, 0 , Math.min(original.length, newLength)); return copy; }
此方法需要两个参数,原始数组,新数组的长度.该方法底层 调用了System.arraycopy ,返回是一个原始数组的副本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.xiebin.day4;import java.util.Arrays;public class ArrayCopy3 { public static void main (String[] args) { int [] arr = new int []{1 ,2 ,3 ,4 ,5 }; int [] arr2 = new int [arr.length]; arr2 = Arrays.copyOf(arr, arr.length); arr[0 ] = 100 ; for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+"\t" ); } System.out.println(); for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]+"\t" ); } System.out.println(); System.out.println("arr2 = " + arr2); System.out.println("arr = " + arr); } } ------------------------------- 100 2 3 4 5 1 2 3 4 5 arr2 = [I@1 b6d3586 arr = [I@4554617 c
Arrays.copyOfRange 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static int [] copyOfRange(int [] original, int from, int to) { int newLength = to - from; if (newLength < 0 ) throw new IllegalArgumentException(from + " > " + to); int [] copy = new int [newLength]; System.arraycopy(original, from, copy, 0 , Math.min(original.length - from, newLength)); return copy;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.xiebin.day4;import java.util.Arrays;public class ArrayCopy3 { public static void main (String[] args) { int [] arr = new int []{1 ,2 ,3 ,4 ,5 }; int [] arr2 = new int [arr.length]; arr2 = Arrays.copyOfRange(arr,0 ,arr.length); arr[0 ] = 100 ; for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+"\t" ); } System.out.println(); for (int i = 0 ; i < arr2.length; i++) { System.out.print(arr2[i]+"\t" ); } System.out.println(); System.out.println("arr2 = " + arr2); System.out.println("arr = " + arr); } } =================== 100 2 3 4 5 1 2 3 4 5 arr2 = [I@1 b6d3586 arr = [I@4554617 c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.xiebin.day4;public class Arraycopy2 { public static void main (String[] args) { String[] arr = new String[]{"AA" ,"BB" ,"CC" ,"DD" ,"EE" ,"FF" }; for (int i = 0 ; i < arr.length/2 ; i++) { String temp = arr[i]; arr[i] = arr[arr.length - i -1 ]; arr[arr.length - i -1 ] = temp; } for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "\t" ); } } }
数组的查找 线性查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.xiebin.day4;public class Arraycopy2 { public static void main (String[] args) { String[] arr = new String[]{"AA" , "BB" , "CC" , "DD" , "EE" , "FF" }; String dest = "EE" ; boolean flag = true ; for (int i = 0 ; i < arr.length; i++) { if (dest.equals(arr[i])) { System.out.println("找到啦,i = " + i); flag = false ; break ; } } if (flag == true ) { System.out.println("对不起,没有找到" ); } } }
二分法查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.xiebin.day4;public class ArraySeach { public static void main (String[] args) { int [] arr = new int []{-98 ,-34 ,2 ,34 ,54 ,66 ,79 ,105 ,210 ,333 }; int dest = -34 ; int head = 0 ; int end = arr.length-1 ; boolean flag = true ; while (head <= end){ int middle = (head + end)/2 ; if (dest == arr[middle]){ System.out.println("找到了指定位置,位置为: " + middle); flag = false ; break ; }else if (arr[middle] > dest){ end = middle -1 ; }else { head = middle + 1 ; } } if (flag){ System.out.println("没有找到" ); } } }
数组的排序 冒泡排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.xiebin.day5;public class ArraySort { public static void main (String[] args) { int [] arr = {43 ,32 ,76 ,-98 ,0 ,64 ,33 ,-21 ,32 ,99 }; for (int i = 0 ; i < arr.length-1 ; i++) { for (int j = 0 ; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1 ]){ int temp = arr[j]; arr[j] = arr[j+1 ]; arr[j+1 ] = temp; } } } for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+"\t" ); } } }