簡體   English   中英

如何使我的Java ArrayList正確顯示?

[英]How do I get my java ArrayList to display properly?

我有一個類,其中用戶輸入用戶確定的雇員數有關雇員的信息(名字和姓氏,地址,雇用日期)。

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

public class Employee {
    public static void main(String [] args){
        String employeeName = null;
        String employeeAddress = null;
        String hireDate = null;

        Scanner userInput = new Scanner(System.in);
        System.out.println("How many employees would you like to enter information for?");
        int numEmployees = userInput.nextInt();

        for( int i = 0; i < numEmployees; i++) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter employees first name: ");
            String firstName = input.nextLine();
            System.out.println("Enter employees last name: ");
            String lastName = input.nextLine();
            System.out.println("Enter street employee lives on:");
            String street = input.nextLine();
            System.out.println("Enter city employee lives in:");
            String city = input.nextLine();
            System.out.println("Enter state employee lives in:");
            String state = input.nextLine();
            System.out.println("Enter employee's zip code:");
            String zip = input.nextLine();
            System.out.println("Enter month employee was hired:");
            String month = input.nextLine();
            System.out.println("Enter day employee was hired:");
            String day = input.nextLine();
            System.out.println("Enter year employee was hired:");
            String year = input.nextLine();

            Name name = new Name(firstName, lastName);
            Address address = new Address(street, city, state, zip);
            Date date = new Date(month, day, year);

            employeeName = name.getName();
            employeeAddress = address.getAddress();
            hireDate = date.getDate();

            ArrayList<String> obj = new ArrayList<String>();
            obj.add(employeeName);
            obj.add(employeeAddress);
            obj.add(hireDate);
            System.out.println(obj);
        }
    }
}

我正在嘗試讓程序通過用戶提示,獲取該信息並將其放在ArrayList中的某個位置,然后重復用戶先前確定的許多次並在最后一次全部顯示。 像這樣:

FirstName LastName, Address, Hire Date
FirstName LastName, Address, Hire Date

等等。 現在,我的代碼將在用戶提示下運行,顯示,然后在下一輪提示下運行並顯示:

Enter Name:
 Name
Enter Address:
 Address
Enter Hire Date:
 Hire Date

[Name, Address, Hire Date]

Enter Name:
 Name
Enter Address:
 Address
Enter Hire Date:
 Hire Date

[Name, Address, Hire Date]

我意識到這是因為我的數組和數組顯示在for循環內,但是當我將數組移到循環外時卻沒有顯示。 當我僅將數組顯示移到循環外時,我的代碼無法編譯。

移動這一行:

System.out.println(obj);

退出for循環。 並使用List包含ArrayList 然后將其顯示在單獨的for循環中:

List<ArrayList<String>> objs = new ArrayList<ArrayList<String>>(); //use list of objs
for( int i = 0; i < numEmployees; i++ )
{
    //all the inputs

    ArrayList<String> obj = new ArrayList<String>();
    obj.add(employeeName);
    obj.add(employeeAddress);
    obj.add(hireDate);
    objs.add(obj);

}

for( int i = 0; i < numEmployees; i++ ) //for loop just to display
    System.out.println(objs.get(i)); //display each element here

您要打印的是一個[string,string,string]類型的元組。 這可以表示為列表。 然后,對於要將此元組存儲在List<List<String>>類型的另一個List中的每個雇員。

public static void main( String [] args )
{

    String employeeName = null;
    String employeeAddress = null;
    String hireDate = null;

    Scanner userInput = new Scanner(System.in);
    System.out.println("How many employees would you like to enter information for?");
    int numEmployees = userInput.nextInt();
    List<List<String>> employesInfo = new ArrayList<List<String>>();

    for( int i = 0; i < numEmployees; i++ )
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter employees first name: ");
        String firstName = input.nextLine();
        System.out.println("Enter employees last name: ");
        String lastName = input.nextLine();
        System.out.println("Enter street employee lives on:");
        String street = input.nextLine();
        System.out.println("Enter city employee lives in:");
        String city = input.nextLine();
        System.out.println("Enter state employee lives in:");
        String state = input.nextLine();
        System.out.println("Enter employee's zip code:");
        String zip = input.nextLine();
        System.out.println("Enter month employee was hired:");
        String month = input.nextLine();
        System.out.println("Enter day employee was hired:");
        String day = input.nextLine();
        System.out.println("Enter year employee was hired:");
        String year = input.nextLine();

        Name name = new Name(firstName, lastName);
        Address address = new Address(street, city, state, zip);
        Date date = new Date(month, day, year);

        employeeName = name.getName();
        employeeAddress = address.getAddress();
        hireDate = date.getDate();

        ArrayList<String> obj = new ArrayList<String>();
        obj.add(employeeName);
        obj.add(employeeAddress);
        obj.add(hireDate);

        employeesInfo.add(obj);
    }

    for (int i = 0; i < numEmployees; i++ ) {
        System.out.println(emploeesInfo.get(i));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM