如何计算java中arraylist中相同数字的出现次数?

时间:2011-06-29 23:29:13

标签: java arraylist

我在java中有一个arraylist,其中的内容是从用户的输入中添加的。用户添加名称,月份编号和年份编号。像这样:姓名:Bob,月:4,年:11

我的任务是查找有多少用户在arraylist中添加了相同的月份编号。计算相同月份数的出现次数并打印出来。

我知道我必须遍历arraylist并存储出现的地方,直到迭代器完成搜索arraylist的集合,然后打印出相同月份数的添加次数。

我很安静地坚持这个任务。即使是艰难也是一件容易的事。

感谢您的帮助!

我有三个班级

public class Person
{
    // The name of this user.
    private final String name;

    /**
     * Create a new user with the given name.
     * @param name The user's name.
     */
    public Person(String name)
    {
        this.name = name;
    }

    /**
     * @return The user's name.
     */
    public String getName()
    {
        return name;
    }
}

/////////////////////////////////

/**
 * Store details of a club membership.
 * 
 */
public class Membership
{
    // The name of the member.
    private String name;
    // The month in which the membership was taken out.
    private int month;
    // The year in which the membership was taken out.
    private int year;

    /**
     * Constructor for objects of class Membership.
     * @param name The name of the member.
     * @param month The month in which they joined. (1 ... 12)
     * @param year The year in which they joined.
     */
    public Membership(String name, int month, int year)
        throws IllegalArgumentException
    {
        if(month < 1 || month > 12) {
            throw new IllegalArgumentException(
                "Month " + month + " out of range. Must be in the range 1 ... 12");
        }
        this.name = name;
        this.month = month;
        this.year = year;
    }

    /**
     * @return The member's name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * @return The month in which the member joined.
     * A value in the range 1 ... 12
     */
    public int getMonth()
    {
        return month;
    }

    /**
     * @return The year in which the member joined.
     */
    public int getYear()
    {
        return year;
    }

    /**
     * @return A string representation of this membership.
     */
    public String toString()
    {
        return "Name: " + name +
               " joined in month " +
               month + " of year " + year;
    }
}

////////////////////////////////////////////

    import java.util.ArrayList;
    import java.util.Iterator;
    /**
     * Store details of club memberships.
     * 
     */
    public class Club
    {
        // Define any necessary fields here ...
        private ArrayList club;
       // private String member = club;
        private int joined; 
        /**
         * Constructor for objects of class Club
         */
        public Club()
        {
            // Initialise any fields here ...
            club = new ArrayList();

        }

        /**
         * Add a new member to the club's list of members.
         * @param member The member object to be added.
         */
        public void join(Membership member)
        {
          club.add(member);
          //System.out.println(member);


        }

//////////////////////////////////////////////////////////    
//    public int getJoined()
//    {
//        return joined;
//    }
//////////////////////////////////////////////////////////    
    /**
     * @return The number of members (Membership objects) in
     * the club.
     */
     public int numberOfMembers()
    {
       return club.size();
    }
///////////////////////////////////////////////////////////////////////////////////    
    public int joinedInMonth(int month)
    {
         //int joined = month;
        if(month < 1 || month > 12)
        {
            System.out.println("Not a valid month");

        }


      else{

//     int countMonth(ArrayList<Person> person, int month)
  {
      int count = 0;
        for (Club club : people)
         if (person.getMonth() == month) count++;
           return count;
} 

        }
        // using traditional for loop
       //  int joined = month;
       // for(int i = 0; i < club.size(); i++)
        //                      {

        //   System.out.println(i+1 + ": " + club.get(i));
         //  }       
//              

//               for(Iterator i = club.iterator();i.hasNext();)
//                  {
//                     
//                    System.out.println(i.next());
//   
//
//          }
         return 0;   
      }

    }

/////////////////////////////////////////

到目前为止,这是我在arraylist中计算相同数字出现次数的方法:

 public int joinedInMonth(int month)
    {
        int joined = month;
        if(joined < 1 || joined > 12){
            System.out.println("Not a valid month");
            }
      else{   
           int count = 0;
          Iterator it = club.iterator();
               while(it.hasNext()) {
          Membership membership = (Membership) it.next();
           if(joined == membership.getMonth());count++;


               System.out.println("Month " + membership.getMonth());
                    return count;

但我无法理解如何将count中的值存储到新的arraylist中? 有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

由于你没有发布2小时,这是一种方式:

class Person {
    String name;
    int month;
    int year;
    // with getters()
}

int countMonth(List<Person> people, int month) {
    int count = 0;
    for (Person person : people)
        if (person.getMonth() == month) count++;
    return count;
}

答案 1 :(得分:0)

对于这种任务,a将使用Hashtable<Integer, List<Person>>,其中特定月份的所有人都将在一个列表中索引。

Hashtable<Integer, List<Person>> index = new Hashtable<Integer, List<Person>>();
for (Person person : persons) {
    if (index.get(person.month) == null) {
        index.put(person.month, new ArrayList<Person>());
    }
    index.get(person.month).add(person);
}

您可以根据月份来检索人员列表。 此代码仅打印月份和列表中包含的人数。

for (int i = 1; i <= 12; i++) {
    int count = 0;
    if (index.get(i) != null) {
        count = index.get(i).size();
    }
    System.out.println("%d: %d", i, count);
}

Hashtables非常适合这类任务,你需要重新组合元素或使用密钥访问值(这里的月份为Integer,值为List<Person>)。 / p>