/*
* 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. 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.
Then use for each loop or iterator over the list and display every object from ArrayList
.
Put a constructor like this in your Individual
class
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
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.
for (Individual individual: individualInfo) {
//individual.getName();
// Like these you can get the properties of individual objects.
}
You can also use Iterator for this purpose.
最好使用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)
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.