简体   繁体   English

如何使Java数组列表具有不同的int和arrayList

[英]How to make a Java array List with different ints and arrayList

I know this may sound very silly but this is my problem. 我知道这听起来很愚蠢,但这是我的问题。

I want a List of people. 我想要一个人List Where each person has following information about them. 每个人都有以下有关他们的信息。

  1. Number of fingers they have (int) 他们的手指数(整数)
  2. Ward numbers they have been so far (it's a list of ints), 到目前为止的病房编号(这是一个整数列表),
  3. Number of days they have been admitted in hospital (int) 他们入院的天数(整数)

I have no idea what to do here. 我不知道该怎么办。 I know how to make 2d array list of ints but the problem is that list of ward number messes everything up. 我知道如何使2d array list of ints但问题是病房号列表使一切搞砸了。

Thanks in advance, 提前致谢,

Create a Person bean class 创建一个Person bean类

public class Person {
  private int fingers;
  private int days;
  private List<Integer> wardNumbers;
  //getter setters

}

And create a List<Person> patientList= new ArrayList<Person> 并创建一个List<Person> patientList= new ArrayList<Person>

So you can store the details as person wise. 因此,您可以根据个人情况存储详细信息。 Or 要么

Create a Map<String,Person> patientMap = new HashMap<String,Person> So you can get the person wise details by patientMap.get("personName"); 创建一个Map<String,Person> patientMap = new HashMap<String,Person>这样您就可以通过patientMap.get("personName");获得有关人员的详细信息patientMap.get("personName");

Update(doesn't recommend) for your restriction(as per comment) : 更新(不推荐)您的限制(根据评论):

/**All information**/
List<Object> personInfo = new ArrayList<Object>();

/**Ward Number**/
List<Integer> wardNum = new ArrayList<Integer>();
wardNum.add(25);
wardNum.add(55);

personInfo.add(5);  //days
personInfo.add(6);  //fingers

personInfo.add(wardNum); //ward number

Map<String,List<Object>> patientMap = new HashMap<String,List<Object>>();
//Store
patientMap.put("patient1", personInfo);

//Retrieve
System.out.println(patientMap.get("patient1").get(0));//days
System.out.println(patientMap.get("patient1").get(1));//fingers
System.out.println(patientMap.get("patient1").get(2));//Ward Number

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

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