ConcurrentModificationException
From: Witold Krakowski (wkrakowski_at_hotmail.com)
Date: 03/05/04
- Next message: Steve W. Jackson: "Re: ConcurrentModificationException"
- Previous message: Jake: "Real time java woes..."
- Next in thread: Steve W. Jackson: "Re: ConcurrentModificationException"
- Reply: Steve W. Jackson: "Re: ConcurrentModificationException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 05 Mar 2004 22:22:11 GMT
I have a problem with a program giving me a
ConcurrentModificationException. Everything works just fine, but I can't
get rid of this exception...Any ideas?
Thanks
import java.util.Comparator;
/**
Classe comparator per oggetti di tipo Student
*/
public class StudentComparator implements Comparator
{
/**
Confronta due oggetti di tipo student
@param o1 il primo studente
@param o2 il secondo studente
@return -1 se il primo oggetto è inferiore, 0 se gli oggetti sono uguali,
1 se il secondo oggetto è inferiore
*/
public int compare(Object o1, Object o2)
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if (s1.getLastName().compareTo(s2.getLastName()) < 0)
return -1;
if (s1.getLastName().compareTo(s2.getLastName()) == 0)
{
if (s1.getFirstName().compareTo(s2.getFirstName()) < 0)
return -1;
if (s1.getFirstName().compareTo(s2.getFirstName()) == 0)
{
if (s1.getId() < s2.getId())
return -1;
if (s1.getId() == s2.getId())
return 0;
return 1;
}
return 1;
}
return 1;
}
}
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.JOptionPane;
/**
Il programma di test per la classe Student.
*/
public class P18_5
{
public static void main(String[] args)
{
Map m = new HashMap();
boolean done = false;
while(!done)
{
String input = JOptionPane.showInputDialog(
"A)gg R)imuovi M)odifica S)tampa");
if (input == null)
done = true;
else if (input.equalsIgnoreCase("A"))
{
boolean done1 = false;
while (!done1)
{
String fName = JOptionPane.showInputDialog(
"inserisci il nome dello studente, Cancel per uscire:");
String lName = JOptionPane.showInputDialog(
"inserisci il cognome dello studente, Cancel per uscire:");
String ident = JOptionPane.showInputDialog(
"inserisci l'ID dello studente, Cancel per uscire:");
String grade = JOptionPane.showInputDialog(
"inserisci il voto dello studente, Cancel per uscire:");
if (fName == null || lName == null
|| ident == null || grade == null)
done1 = true;
else
{
int id = Integer.parseInt(ident);
Student s = new Student(fName, lName, id);
m.put(s, grade);
}
}
}
else if (input.equalsIgnoreCase("R"))
{
boolean done2 = false;
while (!done2)
{
String ident = JOptionPane.showInputDialog(
"inserisci l'ID dello studente, Cancel per uscire:");
if (ident == null)
done2 = true;
else
{
int id = Integer.parseInt(ident);
remove(id, m);
}
}
}
else if (input.equalsIgnoreCase("M"))
{
boolean done3 = false;
while (!done3)
{
String ident = JOptionPane.showInputDialog(
"inserisci l'ID dello studente, Cancel per uscire:");
String grade = JOptionPane.showInputDialog(
"inserisci il voto dello studente, Cancel per uscire:");
if (ident == null || grade == null)
done3 = true;
else
{
int id = Integer.parseInt(ident);
modify(id, grade, m);
}
}
}
else if (input.equalsIgnoreCase("S"))
{
print(m);
}
else
done = true;
}
System.exit(0);
}
/**
Rimuove uno studente dalla mappa.
@param id l'identificativo intero
@param m una mappa
*/
private static void remove(int id, Map m)
{
int studentId = 0;
Set keySet = m.keySet();
Iterator iter = keySet.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Student s = (Student)key;
studentId = s.getId();
if (studentId == id)
m.remove(s);
}
}
/**
Modifica uno studente in una mappa
@param id l'identificativo intero
@param grade il voto dello studente
@param m la mappa
*/
private static void modify(int id, String grade, Map m)
{
int studentId = 0;
Set keySet = m.keySet();
Iterator iter = keySet.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Object value = m.get(key);
Student s = (Student)key;
studentId = s.getId();
if (studentId == id)
m.put(s, grade);
}
}
/**
Stampa il contenuto della mappa
@param m una mappa
*/
private static void print(Map m)
{
List keys = new ArrayList(m.keySet());
Comparator comp = new StudentComparator();
Collections.sort(keys, comp);
Iterator iter = keys.iterator();
while(iter.hasNext())
{
Object key = iter.next();
Object value = m.get(key);
Student s = (Student)key;
System.out.println(s.getFirstName() + " " + s.getLastName() + " " + s.getId()
+ ": " + value);
}
System.out.println("");
}
}
/**
Uno studente con un Identificativo (ID)
*/
public class Student
{
/**
Costruisce un oggetto Studente
@param aFirstName il nome dello studente
@param aLastName il cognome dello studente
@param anId l'ID
*/
public Student(String aFirstName, String aLastName, int anId)
{
firstName = aFirstName;
lastName = aLastName;
id = anId;
}
/**
Restituisce il nome dello studente
@return firstName il nome dello studente
*/
public String getFirstName()
{
return firstName;
}
/**
Restituisce il cognome dello studente
@return lastName il cognome dello studente
*/
public String getLastName()
{
return lastName;
}
/**
Restituisce l'ID dello studente
@return id l'ID
*/
public int getId()
{
return id;
}
private String firstName;
private String lastName;
private int id;
}
- Next message: Steve W. Jackson: "Re: ConcurrentModificationException"
- Previous message: Jake: "Real time java woes..."
- Next in thread: Steve W. Jackson: "Re: ConcurrentModificationException"
- Reply: Steve W. Jackson: "Re: ConcurrentModificationException"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|