在 for 循环内时无法将新元素添加到 Vector 中
Can not add new Element into Vector while inside a for loop
我正在创建一个搜索表单。当用户在 textField 中输入一个字符串并单击按钮搜索时,程序将浏览 Vector,如果 Vector 有任何元素,其名称与输入字符串相同,我将将该元素的名称打印到控制台。
如果 Vector 没有名称与输入字符串相似的元素,我将添加一个名称为输入字符串的新对象到 Vector 中。
例如:当我在文本字段上输入 "John" 并按下搜索按钮时,如果 Vector 中有一个名称为 "John" 的元素,我将在控制台打印 "John"。如果 Vector 没有 "John",我将在 Vector 中添加一个名为 "John" 的新 Student。
我的问题是,我将搜索功能放在 btnSearch 的动作侦听器中,但搜索功能在按钮的动作侦听器中似乎不起作用。我尝试修复它半天,但仍然不明白我做错了什么。所以请帮助我!这是我的整个可运行程序:
* class 包含很多学生的小组:
public class Group {
protected Vector<Student> listStd;
public Group() {
}
public Group(Vector<Student> listStd) {
this.listStd = listStd;
}
public Vector<Student> getListStd() {
return listStd;
}
public void setListStd(Vector<Student> listStd) {
this.listStd = listStd;
}
public void addStudent(Student std){
listStd.add(std);
std.setGr(this);
}
public void search(String name){
for(int i=0;i<this.getListStd().size();i++){
if(this.getListStd().get(i).getName().equalsIgnoreCase(name)){
System.out.println(this.getListStd().get(i).getName());
}
else{
this.getListStd().add(new Student(name));
System.out.println("not found");
}
}
}
public void print(){
for(Student std:this.getListStd()){
System.out.println(std.getName());
}
}
}
class 学生:
public class Student {
protected String name;
protected Group gr;
public Student(){
}
public Student(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public Group getGr() {
return gr;
}
public void setGr(Group gr) {
this.gr = gr;
}
@Override
public String toString() {
return this.getName();
}
}
UI class 执行 UI 和主要功能:
public class UI extends JFrame{
JTextField txtSearch;
Vector<Student> listStd;
JButton btnSearch,btnDisplay;
Group gr;
public UI(String title){
super(title);
addControls();
addEvents();
}
public void addControls(){
listStd = new Vector<Student>();
gr = new Group();
gr.setListStd(listStd);
gr.addStudent(new Student("Kurapika"));
gr.addStudent(new Student("Leorio"));
gr.addStudent(new Student("Hisoka"));
gr.addStudent(new Student("Meruem"));
gr.addStudent(new Student("Gon"));
gr.addStudent(new Student("Killua"));
Container con = getContentPane();
JPanel pnMain = new JPanel();
pnMain.setLayout(new BoxLayout(pnMain, BoxLayout.Y_AXIS));
JPanel pnSearch = new JPanel();
JLabel lblSearch = new JLabel("Write student name to search ");
txtSearch = new JTextField(10);
pnSearch.add(lblSearch);
pnSearch.add(txtSearch);
pnMain.add(pnSearch);
JPanel pnButton = new JPanel();
btnSearch = new JButton("Search");
btnDisplay = new JButton("Display");
pnButton.add(btnSearch);
pnButton.add(btnDisplay);
pnMain.add(pnButton);
con.add(pnMain);
}
public void addEvents(){
btnSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gr.search(txtSearch.getText());
}
});
btnDisplay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gr.print();
}
});
}
public void showWindow(){
this.setSize(300,200);
this.setState(MAXIMIZED_BOTH);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args){
UI ui = new UI("Test");
ui.showWindow();
}
}
它可能(或可能不是)是您问题的根源,但我仍然必须指出您代码中的这个错误:
public void search(String name){
for(int i=0;i<this.getListStd().size();i++){
if(this.getListStd().get(i).getName().equalsIgnoreCase(name)){
System.out.println(this.getListStd().get(i).getName());
}
else{
this.getListStd().add(new Student(name));
System.out.println("not found");
}
}
}
在您的 for
-loop 中,您实际上是在说,对于每个元素,您必须检查其名称是否等于给定名称:如果是,则打印它,否则添加一个新成员。在它之后,转到下一个元素并执行相同的操作。
因此,您正在为 列表中的所有元素 执行此操作。每次有一个元素的名称与参数描述的名称不同时,您都会创建一个新的 Student
。您不应在循环内创建新元素。您不仅多次添加相同的名称,而且还在循环条件中使用它时增加了列表的大小!当您检查完所有元素后,才将元素添加到循环外的列表中。您只能在检查完列表中的所有元素后才能确定该名称不存在。您只需要一个额外的布尔值来记住它是否已找到。所以你的代码应该(至少)是这样的:
public void search(String name){
boolean found = false;
for(int i=0;i<this.getListStd().size();i++){
if(this.getListStd().get(i).getName().equalsIgnoreCase(name)){
System.out.println(this.getListStd().get(i).getName());
found = true;
}
}
if(! found){
this.getListStd().add(new Student(name));
System.out.println("not found");
}
}
这可能(不能)解决您的问题。我至少想指出这个基本错误。
PS:一般来说,如果您在循环条件中使用该大小,请不要更改集合(arraylist、set、...)的大小! (除非它真的真的是你的意图......但这种情况很少见)
祝你好运
我正在创建一个搜索表单。当用户在 textField 中输入一个字符串并单击按钮搜索时,程序将浏览 Vector,如果 Vector 有任何元素,其名称与输入字符串相同,我将将该元素的名称打印到控制台。
如果 Vector 没有名称与输入字符串相似的元素,我将添加一个名称为输入字符串的新对象到 Vector 中。
例如:当我在文本字段上输入 "John" 并按下搜索按钮时,如果 Vector 中有一个名称为 "John" 的元素,我将在控制台打印 "John"。如果 Vector 没有 "John",我将在 Vector 中添加一个名为 "John" 的新 Student。
我的问题是,我将搜索功能放在 btnSearch 的动作侦听器中,但搜索功能在按钮的动作侦听器中似乎不起作用。我尝试修复它半天,但仍然不明白我做错了什么。所以请帮助我!这是我的整个可运行程序:
* class 包含很多学生的小组:
public class Group {
protected Vector<Student> listStd;
public Group() {
}
public Group(Vector<Student> listStd) {
this.listStd = listStd;
}
public Vector<Student> getListStd() {
return listStd;
}
public void setListStd(Vector<Student> listStd) {
this.listStd = listStd;
}
public void addStudent(Student std){
listStd.add(std);
std.setGr(this);
}
public void search(String name){
for(int i=0;i<this.getListStd().size();i++){
if(this.getListStd().get(i).getName().equalsIgnoreCase(name)){
System.out.println(this.getListStd().get(i).getName());
}
else{
this.getListStd().add(new Student(name));
System.out.println("not found");
}
}
}
public void print(){
for(Student std:this.getListStd()){
System.out.println(std.getName());
}
}
}
class 学生:
public class Student { protected String name; protected Group gr; public Student(){ } public Student(String name){ this.name = name; } public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public Group getGr() { return gr; } public void setGr(Group gr) { this.gr = gr; } @Override public String toString() { return this.getName(); } }
UI class 执行 UI 和主要功能:
public class UI extends JFrame{ JTextField txtSearch; Vector<Student> listStd; JButton btnSearch,btnDisplay; Group gr; public UI(String title){ super(title); addControls(); addEvents(); } public void addControls(){ listStd = new Vector<Student>(); gr = new Group(); gr.setListStd(listStd); gr.addStudent(new Student("Kurapika")); gr.addStudent(new Student("Leorio")); gr.addStudent(new Student("Hisoka")); gr.addStudent(new Student("Meruem")); gr.addStudent(new Student("Gon")); gr.addStudent(new Student("Killua")); Container con = getContentPane(); JPanel pnMain = new JPanel(); pnMain.setLayout(new BoxLayout(pnMain, BoxLayout.Y_AXIS)); JPanel pnSearch = new JPanel(); JLabel lblSearch = new JLabel("Write student name to search "); txtSearch = new JTextField(10); pnSearch.add(lblSearch); pnSearch.add(txtSearch); pnMain.add(pnSearch); JPanel pnButton = new JPanel(); btnSearch = new JButton("Search"); btnDisplay = new JButton("Display"); pnButton.add(btnSearch); pnButton.add(btnDisplay); pnMain.add(pnButton); con.add(pnMain); } public void addEvents(){ btnSearch.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { gr.search(txtSearch.getText()); } }); btnDisplay.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { gr.print(); } }); } public void showWindow(){ this.setSize(300,200); this.setState(MAXIMIZED_BOTH); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args){ UI ui = new UI("Test"); ui.showWindow(); } }
它可能(或可能不是)是您问题的根源,但我仍然必须指出您代码中的这个错误:
public void search(String name){
for(int i=0;i<this.getListStd().size();i++){
if(this.getListStd().get(i).getName().equalsIgnoreCase(name)){
System.out.println(this.getListStd().get(i).getName());
}
else{
this.getListStd().add(new Student(name));
System.out.println("not found");
}
}
}
在您的 for
-loop 中,您实际上是在说,对于每个元素,您必须检查其名称是否等于给定名称:如果是,则打印它,否则添加一个新成员。在它之后,转到下一个元素并执行相同的操作。
因此,您正在为 列表中的所有元素 执行此操作。每次有一个元素的名称与参数描述的名称不同时,您都会创建一个新的 Student
。您不应在循环内创建新元素。您不仅多次添加相同的名称,而且还在循环条件中使用它时增加了列表的大小!当您检查完所有元素后,才将元素添加到循环外的列表中。您只能在检查完列表中的所有元素后才能确定该名称不存在。您只需要一个额外的布尔值来记住它是否已找到。所以你的代码应该(至少)是这样的:
public void search(String name){
boolean found = false;
for(int i=0;i<this.getListStd().size();i++){
if(this.getListStd().get(i).getName().equalsIgnoreCase(name)){
System.out.println(this.getListStd().get(i).getName());
found = true;
}
}
if(! found){
this.getListStd().add(new Student(name));
System.out.println("not found");
}
}
这可能(不能)解决您的问题。我至少想指出这个基本错误。
PS:一般来说,如果您在循环条件中使用该大小,请不要更改集合(arraylist、set、...)的大小! (除非它真的真的是你的意图......但这种情况很少见)
祝你好运