Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- // show objects in collections do not need to be immutable
- public class Test
- {
- public int x; // Test instances are mutable
- public static void main(String[] args)
- { // make a list
- ArrayList<Test> list = new ArrayList<>();
- // make some Test instances
- Test obj1 = new Test();
- Test obj2 = new Test();
- Test obj3=new Test();
- // change them
- obj1.x=3;
- obj2.x=4;
- obj3.x=5;
- // put them in the list
- list.add(obj1);
- list.add(obj2);
- list.add(obj3);
- // fetch one
- System.out.println(list.get(2).x); // 5
- // change it
- list.get(2).x=99;
- // fetch it again
- System.out.println(list.get(2).x); // 99
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment