Advertisement
dreamworker

Untitled

Dec 10th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. class Point {
  2.         public int x,y;
  3.  
  4.         public Point(int x, int y) {this.x = x;this.y = y;}
  5.  
  6.         @Override
  7.         public String toString() {
  8.             return "Point{" +
  9.                    "x=" + x +
  10.                    ", y=" + y +
  11.                    '}';
  12.         }
  13.     }
  14.  
  15.     // Testing ---------------------------------------------
  16.  
  17.     List<Point> list = new ArrayList<>();
  18.  
  19.     // populate the list
  20.     list.add(new Point(1,2));
  21.  
  22.     // printing the list
  23.     System.out.println("Initial list: " + list);
  24.  
  25.     // getting unmodifiable list
  26.     List<Point> immutableList = Collections.unmodifiableList(list);
  27.  
  28.     Point p = immutableList.get(0);
  29.     p.x = 10;
  30.  
  31.     // printing the list
  32.     System.out.println("immutableList: " + immutableList.toString());
  33.  
  34.  
  35.  
  36. // RESULT:
  37. Initial list: [Point{x=1, y=2}]
  38. immutableList: [Point{x=10, y=2}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement