• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on ArrayList code  RSS feed

 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the following paragraph taken from the OCA book(page 216) and also look at the code i have attached :
"What happens if you modify the common object references in these lists, myArrList and urArrList ? We have 2 cases here:
  • In the first one, you reassign the object reference using either of the lists. In this case, the value in the second list will remain unchanged.
  • In the second case, you modify the internals of any of the common list elements - in this case, the change will be reflected in both lists."


  • I have written the following code, which tests the first case mentioned above, and i get the output as expected: myarrList remains unchanged. How can i test the second case ? My thoughts are ....'second case is untestable the following code, because String is immutable. I need to use StringBuilder or something else to write code for test of second case mentioned'.


     
    Marshal
    Posts: 56605
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You write your own mutable class and create a List of that class.
    BTW: It should read List<String> myList = new ArrayList<String>();
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!