Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# ConcurrentModificationException

saikiran venkata
Greenhorn

Joined: Oct 31, 2010
Posts: 29

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

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....

SCJP 1.6 | SCWCD 5
The earth is enjoyed by heroes—this is the unfailing truth. Be a hero. Always say, “I have no fear." ~Swami Vivekananda.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 13540

6

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala

I agree. Here's the link: http://aspose.com/file-tools

subject: ConcurrentModificationException