java - 奇怪的 Java ArrayList 行为

标签 java arraylist collections

我正在开发学习 java 的初学者项目。 该项目用于添加、列出和删除人员。就像

输入 1 添加人员 输入 2 列出所有人 输入 3 删除一个人。

我正在使用 ArrayList 集合来处理这个项目。

第一个问题是使用集合是一个好方法吗? 如果是,我将全局声明 ArrayList,但在迭代它时我可以看到它没有存储任何数据。

我的代码如下。

import java.util.*;  

public class OperationClass implements OperationInterface {  
    String id;  
    String fname;  
    String lname;  
    ArrayList<String> Person=new ArrayList<String>();  

    public static void main(String args[]) {  
        new OperationClass().operation();  
    }  

    public static void operation() {  
        int a;  
        Scanner in = new Scanner(System.in);  
        System.out.println("Enter 1 for Adding new Record");  
        System.out.println("Enter 2 to see List of Records");  
        System.out.println("Enter 3 to Delete a record");  
        System.out.println("Enter 4 to Exit!!");  

        a = in.nextInt();  

        if (a == 1) {  
            new OperationClass().addPerson();  
        } if (a == 2) {  
            new OperationClass().listPerson();  
        } if (a == 3) {  
            new OperationClass().deletePerson();  
        } if (a == 4) {  
            System.out.println("Thanks for using the program");  
        }  
    }  

    /** 
     * adds a new Person and stores in the collection 
     */  
    @Override  
    public void addPerson() {  


        Scanner in = new Scanner(System.in);  
new OperationClass().operation();  
        System.out.println("Enter a the id");  
        id = in.next();  

        System.out.println("Enter a the First Name");  
        fname = in.next();  

        System.out.println("Enter a the Last Name");  
        lname = in.next();  


        Person.add(id);  
        Person.add(fname);  
        Person.add(lname);  



        new OperationClass().operation();  
    }  

    @Override  
    public void listPerson() {  
        Iterator itr=Person.iterator();  
        while(itr.hasNext()){  
            System.out.println(itr.next());  
        }  
     new OperationClass().operation();  

    }  

    @Override  
    public void deletePerson() {  
        System.out.println("Delete under construction");  
    }  

    @Override  
    public void quit() {  

    }  
}  

第一个输出是

输入 1 添加新记录。

输入 2 查看记录列表。

输入 3 删除记录。

输入4退出!!

当输入1时,它会像这样

输入 1 添加新记录

输入2查看记录列表

输入3删除记录

输入4退出!!

1

输入 ID

1

输入名字

英地兰

输入姓氏

辛哈

然后,再次按 Enter 后,将出现前四个选项,而进入 2 后,它不会显示任何内容。

输入 1 添加新记录

输入2查看记录列表

输入3删除记录

输入4退出!!

1

输入 ID

1

输入名字

阿斯达斯

输入姓氏

dsffdgdfg

输入 1 添加新记录

输入2查看记录列表

输入3删除记录

输入4退出!!

2

输入 1 添加新记录

输入2查看记录列表

输入3删除记录

输入4退出!!

最佳答案

每次输入数字时,您都会创建一个新的 OperationClass 对象:

if (a == 1) {  
    new OperationClass().addPerson(); //Creating a new object
} if (a == 2) {  
    new OperationClass().listPerson(); //Creating a new object
} if (a == 3) {  
    new OperationClass().deletePerson(); //Creating a new object
} if (a == 4) {  
    System.out.println("Thanks for using the program");  
}  

尝试调用已有对象上的方法:

if (a == 1) {  
    addPerson();  
} if (a == 2) {  
    listPerson();  
} if (a == 3) {  
    deletePerson();  
} if (a == 4) {  
    System.out.println("Thanks for using the program");  
}  

这将调用您在 main() 中创建的对象的方法。

编辑:

正如所指出的,这不会完全起作用,因为您的 operation() 方法是静态的。我建议要么让这个方法成为非静态的(因此是一个实例方法):

public void operation() { 
    //Your operation() code
}

或者将 operation() 的代码一起移动到 OperationClass 的构造函数中,以便在创建该类的对象时它的代码能够正确运行:

public OperationClass() {
    //Your operation() code
}

关于java - 奇怪的 Java ArrayList 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501994/

相关文章:

java - Apache MQ 扫描消息

java - 让线程等待与停止和启动

java - 是否可以在 JTable 中包含 JButton?

.net - 为什么 Array.Sort() 和 Array.IndexOf() 方法是静态的?

java - 当 JToolBar 改变时获取可 float 的位置

Android 从月份名称创建 ArrayList

java - 将数组列表转换为 html 表

Java arraylist - 检查在从列表生成随机整数时是否设置了 arraylist 索引中的两项?

c# - 如何在 C# 中有效地从另一个列表中减去一个巨大的列表

java - 以特定遍历顺序减少/收集展平 map 的流(不是深度优先搜索)