Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:57:59+00:00 2026-05-31T12:57:59+00:00

In this program I keep a list of duelists that kill the best duelist

  • 0

In this program I keep a list of duelists that kill the best duelist when their random int = 0.
I am having trouble with my loop near the bottom and am getting this error:

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at Duelist.main(Duelist.java:74)

Code

import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;

public class Duelist implements Comparable{

private String name;
private int chance;
private boolean alive;


public Duelist(String duelistName, int hitChance){
    alive = true;
    name = duelistName;
    chance = hitChance;

    }
public String getName(){
    return name;
}
public int getChance(){
    return chance;
}
public boolean getLife(){
    return alive;
}
public void kill(){
    alive = false;
}

 public int compareTo(Object anotherDuelist) throws ClassCastException {
        if (!(anotherDuelist instanceof Duelist))
          throw new ClassCastException("A Duelist object expected.");
        int anotherDuelistChance = ((Duelist) anotherDuelist).getChance();  
        return this.chance - anotherDuelistChance;    
      }

public static void main(String[] args){

ArrayList<Duelist> duelers = new ArrayList<Duelist>(5);
//ArrayList<Duelist> personToKill= new ArrayList<Duelist>(5);
ArrayList<Duelist> rank = new ArrayList<Duelist>(5);
Random generator = new Random();




Duelist Antoine = new Duelist("Antoine", 3);
Duelist Francis = new Duelist("Francis", 6);
Duelist Juliette = new Duelist("Juliettee", 1);

duelers.add(Antoine); duelers.add(Francis); duelers.add(Juliette);



//System.out.println(duelers);


//for(Duelist element : duelers){
//  System.out.println(element.getName());
//  System.out.println(element.getChance());
//}
Collections.sort(duelers);
Collections.reverse(duelers);
//for(Duelist element : duelers){
    //System.out.println(element.getName());
    //System.out.println(element.getChance());
//}

while(duelers.size() > 1){


    for(Duelist element : duelers){



            System.out.println(element.getName());
            System.out.println("Chance:" + element.getChance());
            int randomInt = generator.nextInt(element.getChance());
            System.out.println("Random int " + randomInt);



            //Destroy target if randomInt equals 0
            if (randomInt == 0){


                //Check to make sure the best duelist is not killing themselves
                if (element.equals(duelers.get(duelers.size()-1))){
                    System.out.println("LASTDUDE");
                    Duelist bestDueler = duelers.get(duelers.size()-2);
                    bestDueler.kill();
                    rank.add(element);
                    duelers.remove(bestDueler);
                }

                else {
                    System.out.println("Killshot");
                    Duelist bestDueler = duelers.get(duelers.size()-1);
                    bestDueler.kill();
                    rank.add(element);
                    duelers.remove(bestDueler);
                }

            }


    }
}
System.out.println(duelers);


}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:58:00+00:00Added an answer on May 31, 2026 at 12:58 pm
    for(Duelist element : duelers){
    

    While you are inside this block, you are iterating over the duelers list, which means that you can’t change the list without causing a ConcurrentModificationException unless you use the iterator’s remove() method.

    The problem here is, you don’t have access to the iterator. So you’ll have to change your foreach loop to a while loop with an iterator:

    Iterator<Duelist> it = duelists.iterator();
    while(it.hasNext()){
        Duelist element = it.next();
        // continue as before
    

    (update after comments)
    or better:

    for(Iterator<X> it=x.iterator();it.hasNext();){
        Duelist element = it.next();
        //continue as before
    

    and instead of

    duelers.remove(element);
    

    write

    // the problem here is: you can only remove the current element,
    // so you'll have to re-think some of your code
    it.remove();
    

    Or: you could create a copy of your list for iterating. That’s a waste of memory (and a code smell) but if your application is small it should not make much of a difference, and it would require the least amount of rewriting on your part.

    for(Duelist element : new ArrayList<Duelist>(duelers)){
    // continue as above
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to use Date Formatters (NSDateFormatter), but I keep getting this error: Program
Consider this program int main() { float f = 11.22; double d = 44.55;
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
/*This program will ask a user to enter a song list. By doing this
I am creating a linked list program in C and I keep on getting
I am trying to write a linked list program in C, however I keep
I have a program that reads a raw list of in-game entities, and I
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.