Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

C.java 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import java.lang.reflect.*;
  2. public class C {
  3. public static B.D[] arr = new B.D[5];
  4. public static void main(String[]argv) {
  5. arr[0] = new B.D(42);
  6. arr[1] = new B.D(22);
  7. arr[2] = new B.D(46);
  8. arr[3] = new B.D(50);
  9. arr[4] = new B.D(54);
  10. B.D[] arr2 = arr.clone();
  11. // Check the clone is OK
  12. if (arr2[0].i!=42) throw new RuntimeException("Call that a clone 0");
  13. if (arr2[1].i!=22) throw new RuntimeException("Call that a clone 1");
  14. if (arr2[2].i!=46) throw new RuntimeException("Call that a clone 2");
  15. if (arr2[3].i!=50) throw new RuntimeException("Call that a clone 3");
  16. if (arr2[4].i!=54) throw new RuntimeException("Call that a clone 4");
  17. System.err.println("Clone OK - attempting value manipulation");
  18. // Change the clone, check the original is OK
  19. arr2[2] = new B.D(1);
  20. if (arr[2].i == 1) throw new RuntimeException("Shouldnt have affected original");
  21. if (arr2[2].i != 1) throw new RuntimeException("Should have affected clone");
  22. System.err.println("Clone OK - finished");
  23. }
  24. }
  25. class B {
  26. public static class D {
  27. public int i;
  28. D(int x) { i=x;}
  29. }
  30. }