我被困在这个问题上,我做了一个类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