java Java中使用ArrayList存储Name和Age

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5800069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:49:30  来源:igfitidea点击:

Use of ArrayList in Java to store Name and Age

java

提问by Durga Dutt

I want to add both String and Integer type of value in ArrayList.

我想在 ArrayList 中添加 String 和 Integer 类型的值。

For example i have array list in which i want to add name of the persons and age of the corresponding person in a single arralist.

例如,我有一个数组列表,我想在其中添加一个人的姓名和相应人员的年龄。

How can i accomplish this. How can i get this? Actually i am retrieving data from database and want to save all the employee names and ages in a single array list. Like multidimensional array list.

我怎样才能做到这一点。我怎样才能得到这个?实际上,我正在从数据库中检索数据并希望将所有员工姓名和年龄保存在一个数组列表中。像多维数组列表。

回答by Ryan Reeves

Create a a person class with age and name, then create an ArrayList of that type.

创建一个具有年龄和姓名的人员类,然后创建该类型的 ArrayList。

class Person {
  private String mName;
  private int mAge;
  public Person(int age, String name) {
     mName = name;
     mAge = age;
  }
  public String getName() {
     return mName;
  }
  etc ........
}

ArrayList<Person> arr = new ArrayList<Person>();
arr.add(new Person(10, "joe"));

回答by WhiteFang34

You should create a class to represent a person and then add thoseto a list. It's a bad practice to mix the type of objects in a list. You could start with a simple person class:

您应该创建一个类来表示一个人,然后将它们添加到列表中。在列表中混合对象类型是一种不好的做法。你可以从一个简单的 person 类开始:

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

Then create a list like this:

然后创建一个这样的列表:

List<Person> people = new ArrayList<Person>();

And add people that you read from the database to the list:

并将您从数据库中读取的人员添加到列表中:

people.add(new Person(name, age));

回答by rkg

You can use the simple ArrayListand add the boxed type of int, Integeralong with String.

您可以使用 simpleArrayList并添加盒装类型intInteger以及String

回答by Laurence Gonsalves

You can create an ArrayList<Object>. Both Integerand Stringare subclasses of Object, and so both can be added to an ArrayList<Object>.

您可以创建一个ArrayList<Object>. 这两个IntegerString是的子类Object,因此既可以添加到ArrayList<Object>

That said, it seems unlikely to me that you'd really want to do this. An ArrayListis a list. It's generally a bad smell (ie: a sign of a poor design) if you're storing things in a list that aren't the same kind of thing in some sense.

也就是说,在我看来你不太可能真的想要这样做。AnArrayList是一个列表。如果您在列表中存储的东西在某种意义上不是同一种东西,那么这通常是一种难闻的气味(即:设计不佳的迹象)。

Having a correspondence between elements as you describe suggests that you actually want a list of employee objects, where each employee object has a name and an age.

在您描述的元素之间具有对应关系表明您实际上想要一个员工对象列表,其中每个员工对象都有一个姓名和一个年龄。

Another possibility would be to use a Mapif the names are unique. You can use a LinkedHashMapif you care about preserving order.

另一种可能性是使用 aMap如果名称是唯一的。LinkedHashMap如果您关心保留顺序,则可以使用 a 。

回答by Ammu

You can make a Person Class

你可以创建一个 Person 类

class Person{
 String name;
 int age;
 //Add the getteres and setters
}

Then declare an arrayList as follows.

然后声明一个arrayList,如下所示。

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

Then for each person create a person object and populate the arrayList as follows.

然后为每个人创建一个人对象并按如下方式填充 arrayList。

p.add(personObj);

回答by BraedenP

To make sure the compiler doesn't complain, use Generics to specify that you want to add Objects to your ArrayList; this will let you add both types Integer and String to it:

为确保编译器不会报错,请使用泛型指定要将对象添加到 ArrayList;这将允许您向其中添加 Integer 和 String 类型:

ArrayList<Object> myList = new ArrayList<Object>();

ArrayList<Object> myList = new ArrayList<Object>();

Then you can add your objects of either type to it.

然后,您可以向其中添加任一类型的对象。

However, Java is strongly-typed for a reason, so the need to add two types into one ArrayList suggests a design problem to me.

但是,由于某种原因,Java 是强类型的,因此需要将两种类型添加到一个 ArrayList 中,这对我来说是一个设计问题。

回答by Jigar Joshi

class Employee{
int age;
String name;
//+acessor method
}

Lit<Employee> list = new ArrayList<Employee>();