如何在java中的每个arraylist中创建具有不同类型对象的arraylist数组?

时间:2022-01-26 11:58:33

I want to create an array of ArrayList objects.

我想创建一个ArrayList对象数组。

Lets assume the array to be of size 3, so it holds 3 lists. The type of data stored in ArrayList at each index of array is different, like: ArrayList at index 0 contains objects of Class "student", ArrayList at index 1 contains objects of Class "Professor" and ArrayList at index 2 contains objects of Class "Parent".

让我们假设数组的大小为3,因此它包含3个列表。在ArrayList的每个索引处存储在ArrayList中的数据类型是不同的,例如:索引0处的ArrayList包含Class“student”的对象,索引1处的ArrayList包含Class“Professor”的对象,索引2处的ArrayList包含Class“的对象家长”。

How to create it?

如何创建它?

3 个解决方案

#1


1  

Actually, in safelly you can consider object of ArrayList. In detail create like this classes;

实际上,在safelly中你可以考虑ArrayList的对象。详细创建类似这样的类;

Person.class

public class Person{
    //common fields of person
}

Student.class

public class Student extends Person{
  //fields of Student
}

Professeor.class

public class Professor extends Person{
  //fields of Professor
}

Parent.class

public class Parent extends Person{
  //fields of Parent
}

Now you can create Arrays of ArrayList like this from generic class.This implementation is ;

现在你可以从泛型类创建这样的ArrayList数组。这个实现是;

ArrayList<ArrayList<Person>> personList = new ArrayList<>();

//studentList is keeps instance of Person as student
ArrayList<Person> students = new ArrayList<>();
students.add(new Student());
personList.add(students);

//professorsList is keeps instance of Person as professor
ArrayList<Person> professors = new ArrayList<>();
students.add(new Professor());
personList.add(professors);

//parentList is keeps instance of Person as parents
ArrayList<Person> parents = new ArrayList<>();
students.add(new Parent());
personList.add(parents);

For more reads:ArrayList

更多读取:ArrayList

#2


0  

You can use not parametrized ArrayList

您可以使用非参数化的ArrayList

List<List> genericList = new ArrayList<List>();
genericList.add(*new ArrayList()*);

But it is not recommended as you will lose information about a type of each list. (and you have to cast it by yourself)

但不建议这样做,因为您将丢失有关每个列表类型的信息。 (你必须自己施展)

#3


0  

That would look something like this:

这看起来像这样:

import java.util.ArrayList; 
public class HelloWorld{

     public static void main(String []args){
         ArrayList<ArrayList> arrs = new ArrayList<ArrayList>();
         arrs.add(new ArrayList<String>());
         arrs.add(new ArrayList<Integer>());
         arrs.add(new ArrayList<Double>());
         ((ArrayList<String>)arrs.get(0)).add("Hello World!");
        System.out.println(arrs.get(0).get(0));
     }
}

This is not entirely safe to do since you will have to cast the ArrayList with the generic type that you are expecting in each retrieval from the parent ArrayList.

这不是完全安全的,因为您必须使用您在父ArrayList的每次检索中期望的泛型类型来转换ArrayList。

#1


1  

Actually, in safelly you can consider object of ArrayList. In detail create like this classes;

实际上,在safelly中你可以考虑ArrayList的对象。详细创建类似这样的类;

Person.class

public class Person{
    //common fields of person
}

Student.class

public class Student extends Person{
  //fields of Student
}

Professeor.class

public class Professor extends Person{
  //fields of Professor
}

Parent.class

public class Parent extends Person{
  //fields of Parent
}

Now you can create Arrays of ArrayList like this from generic class.This implementation is ;

现在你可以从泛型类创建这样的ArrayList数组。这个实现是;

ArrayList<ArrayList<Person>> personList = new ArrayList<>();

//studentList is keeps instance of Person as student
ArrayList<Person> students = new ArrayList<>();
students.add(new Student());
personList.add(students);

//professorsList is keeps instance of Person as professor
ArrayList<Person> professors = new ArrayList<>();
students.add(new Professor());
personList.add(professors);

//parentList is keeps instance of Person as parents
ArrayList<Person> parents = new ArrayList<>();
students.add(new Parent());
personList.add(parents);

For more reads:ArrayList

更多读取:ArrayList

#2


0  

You can use not parametrized ArrayList

您可以使用非参数化的ArrayList

List<List> genericList = new ArrayList<List>();
genericList.add(*new ArrayList()*);

But it is not recommended as you will lose information about a type of each list. (and you have to cast it by yourself)

但不建议这样做,因为您将丢失有关每个列表类型的信息。 (你必须自己施展)

#3


0  

That would look something like this:

这看起来像这样:

import java.util.ArrayList; 
public class HelloWorld{

     public static void main(String []args){
         ArrayList<ArrayList> arrs = new ArrayList<ArrayList>();
         arrs.add(new ArrayList<String>());
         arrs.add(new ArrayList<Integer>());
         arrs.add(new ArrayList<Double>());
         ((ArrayList<String>)arrs.get(0)).add("Hello World!");
        System.out.println(arrs.get(0).get(0));
     }
}

This is not entirely safe to do since you will have to cast the ArrayList with the generic type that you are expecting in each retrieval from the parent ArrayList.

这不是完全安全的,因为您必须使用您在父ArrayList的每次检索中期望的泛型类型来转换ArrayList。