gpt4 book ai didi

java - 使用数组添加多个类型,多次

转载 作者:行者123 更新时间:2023-11-29 05:43:44 25 4
gpt4 key购买 nike

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package demo;

/**
*
* @author mikel
*/
public class Individual {

private String name;
private String surname;
private String phone;
private String mail;
private String afm;
private int icode;
private int year;
private int month;
private int day;
private int grade;
private int i=1;


/*----------get-----------*/
public String getName()
{
return this.name;
}

public String getSurname()
{
return this.surname;
}

public String getPhone()
{
return this.phone;
}
public String getMail()
{
return this.mail;
}
public String getAfm()
{
return this.afm;
}
public int getiCode()
{
return this.icode;
}
public int getYear()
{
return this.year;
}
public int getMonth()
{
return this.month;
}
public int getDay()
{
return this.day;
}
public int getGrade()
{
return this.grade;
}
/*----------set-----------*/

public void setName(String name)
{
this.name = name;
}

public void setSurname(String surname)
{
this.surname = surname;
}

public void setPhone(String phone)
{
this.phone = phone;
}

public void setAfm(String afm)
{
this.afm = afm;
}

public void setiCode(int icode)
{
this.icode = icode;
}

public void setYear(int year)
{
this.year = year;
}

public void setMonth(int month)
{
this.month = month;
}

public void setDay(int day)
{
this.day = day;
}

public void setGrade(int grade)
{
this.grade = grade;
}

我现在正在学习 Java,我有一个问题,我需要在一个数组中添加多个人的信息,但在这个数组中我想有不止一种类型的信息。我的意思是:

John Smith 964564 email@gg.com 564789

Mikel Nan 589456 email@gg.com 123123

所以结果看起来像一个数组。

我的项目要求程序能够在屏幕上打印我添加的人员的所有姓名和信息的列表。

使用这个作为解决方案,我在另一个答案中看到我没有得到预期的结果。

Object[] obj = new Object[]{name, surname, phone, mail, afm};

我还想在此列表中添加多个人,所以我必须制作更多对象或有其他方法吗?

提前感谢您的宝贵时间!对不起,如果我的解释不是很清楚。

最佳答案

@Mikel 为此目的最好使用 Individual 类对象的 ArrayList。

然后在列表上使用 for each 循环或迭代器并显示 ArrayList 中的每个对象。

在你的 Individual 类中放置一个这样的构造函数

public Individual(String name, String surname, String phone,
String mail, String afm, int icode, int year, int month, int day,
int grade) {


this.name = name;
this.surname = surname;
this.phone = phone;
this.mail = mail;
this.afm = afm;
this.icode = icode;
this.year = year;
this.month = month;
this.day = day;
this.grade = grade;

}

使用这个添加每个人的信息。

像这样声明一个ArrayList

ArrayList<Individual> individualInfo = new ArrayList<Individual>();

每次当你想添加一个新的人信息时,你可以像这样添加

individualInfo.add(new Individual(name, surname, phone, mail, afm, icode, year, month, day, grade));

当您想遍历 individualInfo ArrayList 时,请像这样对每个循环使用。

  for (Individual individual: individualInfo) {

//individual.getName();
// Like these you can get the properties of individual objects.
}

您也可以为此目的使用 Iterator。

关于java - 使用数组添加多个类型,多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com