В цикле я пытаюсь создать новый Объект и добавить его к существующему вектору, но на каждой итерации предыдущие элементы меняются, и все они в конечном итоге становятся одинаковыми. Последний реплицируется. Это как если бы я создавал один и тот же объект или давал ту же ссылку. Но я создаю новый Объект на каждой итерации (ну, я думаю).
static Vector myclients = new Vector();//note : this is an attribute of
//the all class, not just of that method, and I call those methods
// from the main of the same class
while ((strLine = br.readLine()) != null) {
if ( strLine.length() != 0 &&
! strLine.trim().substring(0,1).trim().equals("#")){
// splitting my string
String[] result = strLine.trim().split("\\s+");
int codigo = new Integer (Integer.parseInt(result[0].trim()) ) ;
String nome = new String (result[1].trim() );
try{
if (result[2].trim().equals("cliente")){
Cliente newcliente = new Cliente(codigo, nome);
Interface.err("Before addElement : "+myclientes.toString());
myclientes.addElement(newcliente);
Interface.err("after : "+myclientes.toString() );
}else if (){
// quite the same
}
}catch(Exception e){
Interface.err("pb ... : "+e);
}
} // if
} // while
В моем классе Client много статических элементов:
public class Client {
public static Integer code;
public static String name;
Client(){
code = null;
name = "undefined";
}
Client(Integer code, String name){
this.code = code;
this.name = name;
}
}
И что я получаю:
Before addElement : []
after : [Vincent 0]
Before addElement : [emilie 999]
after : [emilie 999, emilie 999]
Before addElement : [vince 5, vince 5]
after : [bob 5, bob 5, bob 5]
Здесь такой же вопрос
элементы массива дублированы
но это не помогло мне ...
Спасибо за вашу помощь!
ps: я только что попытался создать новое целое число и строку для кода и имени, но, очевидно, это ничего не меняет.