简体   繁体   English

ArrayList对象以分离Array转换

[英]ArrayList object to separate Array conversion

I'm stuck on this problem i make a class Person in which it has only two properties name and age. 我陷入这个问题,我做了一个类Person ,其中只有两个属性name和age。 I'm taking details of that person as a user input and saving as a object in ArrayList but i want to move that ArrayList object properties that user enter from keyboard into separate arrays like all names of person into String array and all ages of person into int array from that ArrayList. 我将那个人的详细信息作为用户输入并保存为ArrayList的对象,但我想将用户从键盘输入的ArrayList对象属性移动到单独的数组中,例如,将人的所有姓名都转换为String数组,并将所有年龄的人转换为该ArrayList中的int数组。 Is there a way to do that? 有没有办法做到这一点?
Code: 码:

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()];

    }

}

There are a lot of ways to do this. 有很多方法可以做到这一点。 One would be to create ArrayLists instead for ages and names and add to them each time new values are pulled in. 一种方法是为年龄和名称创建ArrayLists ,并在每次引入新值时将其添加到它们中。

ArrayList<String> names = new ArrayList<>();
ArrayList<int> ages = new ArrayList<>();

for(int i = 0; i < 2; i++){
    // create person object
    names.add(str);
    ages.add(age);
}

And if they absolutely NEED to be Arrays then you can convert with: 如果绝对需要它们是Arrays则可以转换为:

String[] nameArray = names.toArray(new String[0]);
String[] ageArray = ages.toArray(new int[0]);

I don't know any fancy ways to go about doing this, but a way to do it, whenever you need them, is make a method to loop through all Person objects and make two arrays of the information. 我不知道有什么花哨的方法可以做到这一点,但是,每当需要时,一种实现方法就是创建一个遍历所有Person对象并创建两个信息数组的方法。

So... 所以...

int[] ages = new int[arr.size()];
String[] names = new String[arr.size()];
for(int i = 0; i < arr.size(); i++){
     //assign attributes
}

Then do whatever you want with these arrays... 然后使用这些数组做任何您想做的事情...

Again, there may be a better way to do it, but this will definitel work, although it is not too efficient. 再次,也许有更好的方法可以做到这一点,但这虽然不是很有效,但却可以肯定地工作。


However, in your for loop, right where you add the Person objects to the array, you can just add them to a declared arraylist in the beginning. 但是,在for循环中,就在将Person对象添加到数组的位置,您可以只在开始时将它们添加到已声明的arraylist中。

Then if you truly need it to be a "List" as opposed to "ArrayList", you can convert the arraylist to a list. 然后,如果您真正需要将其作为“列表”而不是“ ArrayList”,则可以将arraylist转换为列表。 Here is a good example of that. 是一个很好的例子。

If you want to hold your arraylist with person-objects and fill the arrays after all values read in create a new function: 如果要保留带有人员对象的数组列表并在读取所有值之后填充数组,请创建一个新函数:

private void add2Arrays(List<Person> list) {
  for(int i = 0; i < list.size(); i++) {
    Person p = list.get(i);
    stringarray[i] = p.getName();
    intarray[i] = p.getAge();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM