从类切换到类时,为什么我丢失了ArrayList的内容?

时间:2019-11-30 04:03:17

标签: java oop object arraylist

我目前遇到问题,同时还在为ArrayList挣扎。我在使用ArrayList时被卡住了。我正在研究一个三层程序,其中包括客户端,目录和人员类。我的代码读取文件的内容,然后将它们首先添加到ArrayList中,效果很好。在某一时刻,当它不断地从Client或另一个类来回切换时,ArrayList的内容消失了。例如,当我尝试打印时,就不再有打印内容,因此我的问题就出现了。此外,当仅使用一个类并排除其他类时,在对数据进行硬编码时处理数据没有问题。打印它。为了更好地了解它,下面有一些代码。

public class Client 
{

    public static void main (String [] args) throws FileNotFoundException
    {
        Directory print = new Directory();
        Directory loadList = new Directory();

        System.out.print("Please enter the file name: ");
        fileName = kb.nextLine();
        fileName = fileName + ".txt";
        inFile = new File(fileName);
        in = new Scanner(inFile);
        loadList.loadData(in);
        showMenu();

        System.out.println();

        selection = '\0';
        while(selection != 'E')
        {
            System.out.print("Please choose one of choices: ");
            choice = kb.next();

            selection = choice.toUpperCase().charAt(0);

            switch(selection)
            {
                case 'P':
                    print.printDirectory();
                    break;
                case 'L':   
                    System.out.println("Search!");
                    break;
                //some codes here for selection process 
            }
        }

        System.out.println ("Thank you!");
    }
}

当我选择打印ArrayList的内容时,其所有内容均已消失。我只剩下标题和一个空列表。我认为通过将ArrayList用作实例变量,它可以正常工作,但事实并非如此。

public class Directory
{

    private ArrayList<Person> directory = new ArrayList<Person>();

    public void run() throws FileNotFoundException
    {   
    }
    public void loadData(Scanner in) throws FileNotFoundException
    {
        String firstName, ini, lastName, dep, phoneNo;
        while(in.hasNext())
        {
            fName = in.next();
            ini = in.next();
            lName = in.next();
            dep = in.next();
            phoneNo = in.next();

            Person personDetails = new Person(fName, ini, lName, dep, phoneNo;

            directory.add(personDetails);
        }   
    }
    public void printDirectory()
    {

        System.out.format("%s%28s%22s%n","Name","Department","Phone Number");
        for(Person dir: directory)
        {
            System.out.println(dir.toString());
        }
    }
}

如果有人可以进一步向我解释,请多谢。当我从菜单中选择过程时,返回到Client并从Directory类中调用一个方法时,是否丢失了ArrayList中的所有数据?

1 个答案:

答案 0 :(得分:2)

您需要从将对象放入ArrayList的同一对象中读取

public static void main (String [] args) throws FileNotFoundException
{
    Directory dir= new Directory();

    System.out.print("Please enter the file name: ");
    fileName = kb.nextLine();
    fileName = fileName + ".txt";
    inFile = new File(fileName);
    in = new Scanner(inFile);
    dir.loadData(in);
    showMenu();

    System.out.println();

    selection = '\0';
    while(selection != 'E')
    {
        System.out.print("Please choose one of choices: ");
        choice = kb.next();

        selection = choice.toUpperCase().charAt(0);

        switch(selection)
        {
            case 'P':
                dir.printDirectory();
                break;
            case 'L':   
                System.out.println("Search!");
                break;
            //some codes here for selection process 
        }
    }

    System.out.println ("Thank you!");
}