我被困在這個問題上,我做了一個類Person
其中它只有兩個屬性名稱和年齡。我將該人的詳細信息作爲用戶輸入並保存爲ArrayList
中的對象,但我想將該用戶從鍵盤輸入的對象屬性移動到單獨的數組中,如將所有姓名人物排入String
數組以及所有年齡段的人該數組列表中的int
數組。有沒有辦法做到這一點?
代碼:ArrayList對象來分開數組轉換
public class Person {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
ArrayList<Person> arr = new ArrayList<Person>();
Person person = null;
for(int i=0;i<2;i++){
person = new Person();
System.out.println("Enter Person "+(i+1)+" Record");
int age = 0;
System.out.print("Enter Name : ");
String str = s.nextLine();
person.name = str;
System.out.print("Enter Age : ");
age = s.nextInt();
s.nextLine();
person.age = age;
arr.add(person);
person = null;
System.out.println("Record "+(i+1)+" Successful Fill");
}
String[] stringarray = new String[arr.size()];
int[] intarray = new int[arr.size()];
}
}
是的,有一種方法。 – csmckelvey
它是什麼邏輯? – hamel123