i run following codes in eclipse:
ArrayList<StringBuilder> list = new ArrayList<StringBuilder>();
ArrayList<Integer> alist = new ArrayList<Integer>();
// add some elements ti list
list.add(new StringBuilder("hello"));
list.add(new StringBuilder("2"));
list.add(new StringBuilder("hi"));
list.add(new StringBuilder("this"));
// add some elements to alist
alist.add(4);
alist.add(9);
//get method
StringBuilder a = list.get(3);
a.append(" is a good day");
int b = alist.get(1);
b = 7;
// print the list
System.out.println("LinkedList:" + list);
System.out.println("ArrayList:" + alist);
and result is here
LinkedList:[hello, 2, hi, this is a good day]
ArrayList:[4, 9]
It looks like get method returns a shallow copy of list element (in the case of StringBuilder) to the a, but returns a deep copy (in the case of integer) to the b! why it happened? Do the get method return a deep or shallow copy of list's elements?