2017-03-11 111 views
0

我怎樣才能將下面的代碼存儲在使用Java的數組列表中?我卡住了。對你的幫助表示感謝。存儲在ArrayList中

System.out.println("Please enter Officer's First Name"); 
firstName = input.next(); 
officerObject.setOfficerFirstName(firstName); 
System.out.println("Enter Officer's Last Name"); 
lastName = input.next(); 
officerObject.setOfficerLastName(lastName); 
System.out.println("Enter Officer's Badge Number"); 
badgeNum = input.next(); 
officerObject.setOfficerBadgeNum(badgeNum); 
System.out.println("Enter Officer's Precint"); 
precint = input.next(); 
officerObject.setOfficerPrecint(precint); 
+0

什麼是'officerObject'?你想要存儲多少? –

+1

@ElliottFrisch **什麼是officerObject **,只有James_Bond知道:) – ShayHaned

+2

您不會將* code *存儲在'ArrayList'中,您存儲* data *,並且您通常通過調用'add() '方法。創建'ArrayList'和/或調用'add()'的哪一部分讓你感到困擾? – Andreas

回答

2

List<YOUR OBJECT TYPE> array = new ArrayList<>();創建一個數組列表,然後使用array.add(officerObject);將其添加到數組列表。

0

嘛,他說,使用列表中查找

首先它可以像這樣使用:

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 
    Officer officerObject = new Officer(); 

    System.out.println("Please enter Officer's First Name"); 
    officerObject.setOfficerFirstName(input.next()); 
    System.out.println("Enter Officer's Last Name"); 
    officerObject.setOfficerLastName(input.next()); 
    System.out.println("Enter Officer's Badge Number"); 
    officerObject.setOfficerBadgeNum(input.next()); 
    System.out.println("Enter Officer's Precint"); 
    officerObject.setOfficerPrecint(input.next()); 

    List<Officer> officer = new ArrayList<Officer>(); 
    officer.add(officerObject); 

    for(Officer of: officer){ 
     System.out.println(of.getOfficerFirstName()); 
     System.out.println(of.getOfficerLastName()); 
     System.out.println(of.getOfficerBadgeNum()); 
     System.out.println(of.getOfficerPrecint()); 
    }