Hello, I'm here trying to publish items from the list and print 5 for each iteration. It's printing the same item every time in the following code
for (int i = 0; i <4+ i ++) {list & lt; Cards & gt; L = a.subList (a.size () - 5, a.size ()); Println (L); } But here it is printing different items as if it is removing 5 from the list every time
for (int i = 0; i & lt; 4; i ++) {int deck seas = a. Siege (); & Lt; Cards & gt; HandView = a.subList (deck size-5, deck size); ArrayList & LT; Cards & gt; Hands = new arreelist & lt; Cards & gt; (Hand sight); HandView.clear (); Println (hand); } What is the difference between the above two code snippets
You should read one on this.
The returned list is supported from this list, so non-structural changes in the returned list appear in this list, and vice versa.
So the list you are creating in each case is not a new copy of the elements from the original list, but there is only one view in the original list. In the second example, you are clearly calling on the new list, which are actually clearing those elements in the original list, so you are seeing the behavior.
Comments
Post a Comment