1
/*
 * 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};

另外我想在这个列表中添加多个人,所以这样我必须制作更多的对象还是有另一种方法?

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

4

3 回答 3

3

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

然后在列表上使用 for 每个循环或迭代器并显示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;

}

使用此添加每个个人信息。

声明一个ArrayListlike this

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 来达到这个目的。

于 2013-05-15T18:22:23.693 回答
0

最好使用 List 然后你可以简单地使用方法 .add(new Individual()

于 2013-05-15T18:22:48.220 回答
0

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)
    );
于 2013-05-15T18:25:16.400 回答