【问题标题】:Use an arraylist multiple times inside different parts of JAVA code在 JAVA 代码的不同部分中多次使用数组列表
【发布时间】:2021-05-02 14:45:44
【问题描述】:

我正在使用数组列表来存储来自用户输入的值,方法是构建一个交互式菜单供他们选择。到目前为止,我的两个选择是让用户向列表中输入数据并阅读列表的全部内容。到目前为止,我创建的代码包含两个类。

我的主要课程,

package com.andrekreou;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to the personnel address book");
        System.out.println("In the following menu, a whole selection of services is provided");
        Scanner user_input = new Scanner(System.in);
        while (true){
            showMenu();
            String selection = user_input.next();

            if (selection.equals("1")){
                System.out.println("Below you can see all of the data being provided");

                for (String personnel : catalog) { }  //ERROR: Cannot resolve symbol catalog

            }else if (selection.equals("2")){
                ArrayList<String> catalog = new ArrayList<>();
                Personnel p1 = new Personnel();
                System.out.println("Please insert the data for the new contact");

                System.out.println("Input the fullname:");
                Scanner scan = new Scanner(System.in);
                String full_name = scan.nextLine();
                p1.setFull_name(full_name);
                catalog.add(full_name);
                System.out.println("You inserted the following fullname: "+p1.getFull_name());

                System.out.println("Input the phonenumber:");
                Scanner phone_number_input = new Scanner(System.in);
                String phone_number = phone_number_input.next();
                p1.setPhone_number(phone_number);
                catalog.add(phone_number);
                System.out.println("You inserted the following phonenumber: "+p1.getPhone_number());

                System.out.println("Input the address:");
                Scanner address_input = new Scanner(System.in);
                String address = address_input.nextLine();
                p1.setAddress(address);
                catalog.add(address);
                System.out.println("You inserted the following address: "+p1.getAddress());

                System.out.println("Εισάγετε την διεύθυνση e-mail:");
                Scanner email_input = new Scanner(System.in);
                String email = email_input.next();
                p1.setEmail(email);
                catalog.add(email);
                System.out.println("You inserted the following e-mail: "+p1.getEmail());

                System.out.println("Εισάγετε την ημερομηνία γέννησης:");
                Scanner date_of_birth_input = new Scanner(System.in);
                String date_of_birth = date_of_birth_input.nextLine();
                p1.setDate_of_birth(date_of_birth);
                catalog.add(date_of_birth);
                System.out.println("You inserted the following: "+p1.getDate_of_birth());

                System.out.println("Εισάγετε τον αριθμό ΑΜΚΑ:");
                Scanner AMKA_input = new Scanner(System.in);
                String AMKA = AMKA_input.next();
                p1.setAMKA(AMKA);
                catalog.add(AMKA);
                System.out.println("You inserted the following ΑΜΚΑ: "+p1.getAMKA());

            }
        }
    }
    static void showMenu(){
        System.out.println("1. View the whole contacts");
        System.out.println("2. Insert a new contact");
        System.out.println("Please give your choice");
    }
}

我的人事类使用 getter 和 setter 方法来存储来自用户输入的数据,

package com.andrekreou;

import java.io.Serializable;

public class Personnel implements Serializable {
    private String full_name;
    private String phone_number;
    private String address;
    private String email;
    private String date_of_birth;
    private String AMKA;

    public String getFull_name() {
        return full_name;
    }

    public void setFull_name(String full_name) {
        this.full_name = full_name;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDate_of_birth() {
        return date_of_birth;
    }

    public void setDate_of_birth(String date_of_birth) {
        this.date_of_birth = date_of_birth;
    }

    public String getAMKA() {
        return AMKA;
    }

    public void setAMKA(String AMKA) {
        this.AMKA = AMKA;
    }
}

我的问题是我想通过 foreach 循环使用选项 1 中的目录列表,但我不能,因为我收到代码中显示的“无法解析符号目录”错误。我做错了什么?

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    我不会争论解决方案的正确性以及实现它的不同方式。但是,如果你想解决那个编译错误,这只是一个变量范围的问题:你只需要将catalog列表的创建移动到main函数的开头,以增加其范围,例如你可以把它作为第一个语句,像这样:

    public class Main {
       
       public static void main(String[] args) {
          ArrayList<String> catalog = new ArrayList<>();  // Creation of catalog list
          ...
       }
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多