- <span style="font-size:16px;">package test;
- public class SystemDemo {
- public static void main(String[] args) {
- String[] s = new String[] { "liu" };
- String[] s2 = new String[] { "hai" };
- String[][] a = { s, s2, s, s, s, s2, s2, s };
- String[][] b = new String[10][10];
- /*
- * System类包含一些有用的类字段和方法,是final类,无法被继承,构造方法是private,不能创建System对象。
- * 所有public属性和方法都是final static
- */
- // 1.数组复制,采用本地方法复制,实现了深拷贝
- System.arraycopy(a, 1, b, 0, 5);
- System.out.println(b[0][0]);
- // 2.已经过去的毫米数,从1970-1-1开始
- System.currentTimeMillis();
- // 3.提示虚拟机进行垃圾回收,通过调用Runtime.getRuntime().gc();实现
- System.gc();
- // 4.返回系统环境
- System.getenv();
- // 5.返回当前的系统属性。
- System.getProperties();
- // 6.可用於計數已過去的時間
- System.nanoTime();
- // 7.0表示正常退出,調用Runtime.getRuntime().exit(0);
- System.exit(0);
- // 8.返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。
- System.identityHashCode(null);
- }
- }
- </span>
- <span style="font-size:16px;">package test;
-
- public class ObjectDemo {
- /*
- * 1.Object没有public 的静态属性和方法
- * 2.public final native Class<?> getClass()返回运行时类信息,
- * 3.toString 返回类名+@+哈希码值
- * 4.其中wait和notify方法是final的,不可继承
- * 5.equals方法只比较对象的引用,hashCode方法返回哈希码值。
- * 6.重写equals方法要重写hashCode,因为相同的对象(通过equals比较返回true)
- * 必须返回相同的哈希码。
- * 7.finalize方法是一个protected空方法
- * 8.protected native Object clone()返回一个副本,对象必须实现Cloneable接口
- */
- }
- </span>
- <span style="font-size:16px;">package test;
-
- import java.util.Arrays;
-
- public class ArraysDemo {
- /*
- * 1.数组类提供了排序功能,对基本数据类型length<7采用直接插入排序,否则采用快速排序 如果数组元素时对象,采用合并排序
- * 2.提供二分查找法实现,注意二分查找时先对数组进行排序,否则返回一个不确定值
- */
- public static void main(String[] args) {
- int[][] a = { { 1, 2 } };
- int[][] b = { { 1, 2 } };
- System.out.println(Arrays.toString(a));
- // 3. 多维数组的toString
- System.out.println(Arrays.deepToString(a));
- System.out.println(Arrays.hashCode(a));
- System.out.println(Arrays.deepHashCode(a));
- System.out.println(Arrays.equals(a, b));
- // 4. 多维数组的比较
- System.out.println(Arrays.deepEquals(a, b));
-
- // 5. 并没有实现多维数组的复制
- int[][] c = Arrays.copyOf(a, 1);
- // 6.填充数组
- Arrays.fill(a[0], 5);// 在此改变a的值影响到了数组c的值
- System.out.println(Arrays.deepToString(c));
- System.out.println(Arrays.equals(a, c));
- System.out.println(Arrays.deepEquals(a, c));
-
- }
- }
- </span>
- <span style="font-size:16px;">package test;
-
- public class DeepCloneDemo {
- public static void main(String[] args) {
- B b = new B(2, new A(1));
- B b1 = (B) b.clone();
- System.out.println(b == b1);
- System.out.println(b.equals(b1));
- System.out.println(b.getClass() == b.getClass());
- System.out.println("改变b的副本b1前:y=" + b.getY() + ",x=" + b.getA().getX());
- b1.setY(5);
- b1.getA().setX(100);
- System.out.println("改变b的副本b1后:y=" + b.getY() + ",x=" + b.getA().getX());
- System.out.println("深克隆成功!!!");
- }
- }
-
- class A implements Cloneable {
- private int x;
-
- // 为了实现深克隆
- public Object clone() {
- A a = null;
- try {
- a = (A) super.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return a;
- }
-
- public A(int x) {
- this.x = x;
- }
-
- public int getX() {
- return x;
- }
-
- public void setX(int x) {
- this.x = x;
- }
- }
-
- class B implements Cloneable {
- private int y;
- private A a;
-
- // 覆盖Object中clone方法
- // protected native Object clone() throws CloneNotSupportedException;
- // 注意到protected,这里把权限改为了public
- public Object clone() {
- B b = null;
- try {
- b = (B) super.clone();
- // 实现深克隆,没有这条语句只是克隆了a的引用
- b.a = (A) a.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return b;
- }
-
- public B(int y, A a) {
- this.y = y;
- this.a = a;
- }
-
- public int getY() {
- return y;
- }
-
- public A getA() {
- return a;
- }
-
- public void setY(int y) {
- this.y = y;
- }
-
- public void setA(A a) {
- this.a = a;
- }
- }
- </span>
- <span style="font-size:16px;">package test;
- public class SystemDemo {
- public static void main(String[] args) {
- String[] s = new String[] { "liu" };
- String[] s2 = new String[] { "hai" };
- String[][] a = { s, s2, s, s, s, s2, s2, s };
- String[][] b = new String[10][10];
- /*
- * System类包含一些有用的类字段和方法,是final类,无法被继承,构造方法是private,不能创建System对象。
- * 所有public属性和方法都是final static
- */
- // 1.数组复制,采用本地方法复制,实现了深拷贝
- System.arraycopy(a, 1, b, 0, 5);
- System.out.println(b[0][0]);
- // 2.已经过去的毫米数,从1970-1-1开始
- System.currentTimeMillis();
- // 3.提示虚拟机进行垃圾回收,通过调用Runtime.getRuntime().gc();实现
- System.gc();
- // 4.返回系统环境
- System.getenv();
- // 5.返回当前的系统属性。
- System.getProperties();
- // 6.可用於計數已過去的時間
- System.nanoTime();
- // 7.0表示正常退出,調用Runtime.getRuntime().exit(0);
- System.exit(0);
- // 8.返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。
- System.identityHashCode(null);
- }
- }
- </span>
- <span style="font-size:16px;">package test;
- public class ObjectDemo {
- /*
- * 1.Object没有public 的静态属性和方法
- * 2.public final native Class<?> getClass()返回运行时类信息,
- * 3.toString 返回类名+@+哈希码值
- * 4.其中wait和notify方法是final的,不可继承
- * 5.equals方法只比较对象的引用,hashCode方法返回哈希码值。
- * 6.重写equals方法要重写hashCode,因为相同的对象(通过equals比较返回true)
- * 必须返回相同的哈希码。
- * 7.finalize方法是一个protected空方法
- * 8.protected native Object clone()返回一个副本,对象必须实现Cloneable接口
- */
- }
- </span>
- <span style="font-size:16px;">package test;
- import java.util.Arrays;
- public class ArraysDemo {
- /*
- * 1.数组类提供了排序功能,对基本数据类型length<7采用直接插入排序,否则采用快速排序 如果数组元素时对象,采用合并排序
- * 2.提供二分查找法实现,注意二分查找时先对数组进行排序,否则返回一个不确定值
- */
- public static void main(String[] args) {
- int[][] a = { { 1, 2 } };
- int[][] b = { { 1, 2 } };
- System.out.println(Arrays.toString(a));
- // 3. 多维数组的toString
- System.out.println(Arrays.deepToString(a));
- System.out.println(Arrays.hashCode(a));
- System.out.println(Arrays.deepHashCode(a));
- System.out.println(Arrays.equals(a, b));
- // 4. 多维数组的比较
- System.out.println(Arrays.deepEquals(a, b));
- // 5. 并没有实现多维数组的复制
- int[][] c = Arrays.copyOf(a, 1);
- // 6.填充数组
- Arrays.fill(a[0], 5);// 在此改变a的值影响到了数组c的值
- System.out.println(Arrays.deepToString(c));
- System.out.println(Arrays.equals(a, c));
- System.out.println(Arrays.deepEquals(a, c));
- }
- }
- </span>
- <span style="font-size:16px;">package test;
- public class DeepCloneDemo {
- public static void main(String[] args) {
- B b = new B(2, new A(1));
- B b1 = (B) b.clone();
- System.out.println(b == b1);
- System.out.println(b.equals(b1));
- System.out.println(b.getClass() == b.getClass());
- System.out.println("改变b的副本b1前:y=" + b.getY() + ",x=" + b.getA().getX());
- b1.setY(5);
- b1.getA().setX(100);
- System.out.println("改变b的副本b1后:y=" + b.getY() + ",x=" + b.getA().getX());
- System.out.println("深克隆成功!!!");
- }
- }
- class A implements Cloneable {
- private int x;
- // 为了实现深克隆
- public Object clone() {
- A a = null;
- try {
- a = (A) super.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return a;
- }
- public A(int x) {
- this.x = x;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- }
- class B implements Cloneable {
- private int y;
- private A a;
- // 覆盖Object中clone方法
- // protected native Object clone() throws CloneNotSupportedException;
- // 注意到protected,这里把权限改为了public
- public Object clone() {
- B b = null;
- try {
- b = (B) super.clone();
- // 实现深克隆,没有这条语句只是克隆了a的引用
- b.a = (A) a.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- return b;
- }
- public B(int y, A a) {
- this.y = y;
- this.a = a;
- }
- public int getY() {
- return y;
- }
- public A getA() {
- return a;
- }
- public void setY(int y) {
- this.y = y;
- }
- public void setA(A a) {
- this.a = a;
- }
- }
- </span>