[英]Use array to add more than one type, multiple times
/*
* 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;
}
I'm learning Java right now, and I had a question, I need to add information for more than one person in an array, but in this array I would like to have more than one type of info. 我现在正在学习Java,我有一个问题,我需要在一个数组中为多个人添加信息,但是在这个数组中,我希望有多种类型的信息。 What I mean:
我的意思是:
John Smith 964564 email@gg.com 564789
Mikel Nan 589456 email@gg.com 123123
So the result looks like an array. 所以结果看起来像一个数组。
My project asks the program to be able to print on screen a list of all names and info of people that I add. 我的项目要求程序能够在屏幕上打印我添加的所有人名和信息列表。
Using this as a solution, that I saw in an other answer I don't get the expected result. 使用这个作为解决方案,我在另一个答案中看到,我没有得到预期的结果。
Object[] obj = new Object[]{name, surname, phone, mail, afm};
Also I want to add more than one person in this list, so in that way I have to make more objects or there is another way? 另外我想在这个列表中添加多个人,所以这样我必须制作更多的对象,还是有另一种方式?
Thank you in advance for your time! 提前谢谢您的时间! Sorry if my explanation isn't so clear.
对不起,如果我的解释不那么清楚。
@Mikel its better to use ArrayList of Individual
class objects for this purpose. @Mikel为此目的更好地使用
Individual
类对象的ArrayList。
Then use for each loop or iterator over the list and display every object from ArrayList
. 然后在列表上使用每个循环或迭代器,并显示
ArrayList
每个对象。
Put a constructor like this in your Individual
class 将这样的构造函数放在
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;
}
Add every Individual person info by using this. 使用此功能添加每个人的个人信息。
Declare an ArrayList
Like thiis 像thiis一样声明一个
ArrayList
ArrayList<Individual> individualInfo = new ArrayList<Individual>();
Every time when you want to add an new person info you can add like these 每当您想要添加新人信息时,您都可以添加这些信息
individualInfo.add(new Individual(name, surname, phone, mail, afm, icode, year, month, day, grade));
when you want to iterate over the individualInfo ArrayList
use for each loop like this. 当你想迭代使用
individualInfo ArrayList
的每个循环时。
for (Individual individual: individualInfo) {
//individual.getName();
// Like these you can get the properties of individual objects.
}
You can also use Iterator for this purpose. 您也可以将Iterator用于此目的。
最好使用List然后你可以简单地添加使用方法.add(new Individual()
You need a constructor: 你需要一个构造函数:
Individual []individuals = new Individual[] {
new Individual(name1, surname1, phone1, mail1, afm1),
new Individual(name2, surname2, phone2, mail2, afm2),
new Individual(name3, surname3, phone3, mail3, afm3)
};
Add it to a list with: 将其添加到列表中:
ArrayList<Individual> listIndividuals = new ArrayList<Individual>(
Arrays.asList(individuals)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.