• Post Reply Bookmark Topic Watch Topic
  • New Topic

ConcurrentModificationException  RSS feed

 
saikiran venkata
Greenhorn
Posts: 29
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone...i am writing a program for :

You are given a pack of cards containing X unique cards.

You are given a number Y that is less than X

we have to return return noOfShuffles that required in order to return the pack of cards to its original order...

A shuffle is defined as :

-->cut the pack Y cards from the top forming two portions : the top portion and bottom portion

-->put down the the bottom card from the top portion and then the bottom card from the bottom portion

-->continue in this manner altering cards until one portion is used up

-->the remaining cards go top

for example.

take X = 5,Y =2


5 unique cards : 0 1 2 3 4

cut : 0
1
-----
2
3
4


put down bottom card fro each portion

0 | 2 3 4 1

repeat until one portion is used up


| 2 3 0 4 1
remaining cards go up

2
3
0
4
1

this is not original order perform once again shuffle......until original order

o
1
2
3
4 to come....
we have to return how many shuffles it took


and my code is..







i have used LinkedList for containing X unique cards,
after cutting the pack witha given number ,i stored the top portion in
another LinkedList....

i put down the bottom cards from two portions in another Arraylist..,

my pgm caused ConcurrentModificationException in below code




this code working fine in first shuffle....but causing Exception in
second shuffle....


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get a ConcurrentModificationException if you change the content of the list while you are iterating over it. In your code, you are adding elements to the list inside the iteration loop.

Instead of doing that, you'll have to find a different solution. For example, add the elements that you want to add to a new, separate list, and after the loop add all the elements to the original list.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!