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

在java中,如何计算arraylist中相同数字的出现次数?,java,arraylist,Java,Arraylist,我有一个java的arraylist,其中的内容是通过用户的输入添加的。用户可以添加姓名、月份编号和年份编号。比如:姓名:鲍勃,月份:4,年份:11 我的任务是找出有多少用户在arraylist中添加了相同的月号。计算同一个月数的发生次数并打印出来 我知道我必须遍历arraylist并将发生的事件存储在何处,直到迭代器完成对arraylist集合的搜索,然后打印出添加了多少次相同的月数 我被这项任务困住了。即使困难,这也是一项容易的任务 谢谢你的帮助 我有三节课 public class Per

我有一个java的arraylist,其中的内容是通过用户的输入添加的。用户可以添加姓名、月份编号和年份编号。比如:姓名:鲍勃,月份: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;   
      }

    }
////////////////////////////////////////////

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

    }
/////////////////////////////////////////

/**
 * 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中?
有什么帮助吗?

因为你已经两个小时没有发帖了,这里有一个方法:

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;
}

对于这类任务,a将使用一个哈希表,其中特定月份的所有人都将在一个列表中索引到一起

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);
}

哈希表非常适合此类任务,如果您必须将元素重新组合在一起或使用键访问值,则此处的月份为整数,值为List。

您已经正确理解了这一点。。你困在哪里?听起来像是家庭作业。它是?到目前为止你试过什么?你得到了什么样的输出,与你期望的有什么不同?@M.Sameer几年前我在大学里上过java课程。我知道我正在努力更新对java的一些基本理解。我试图通过arraylist进行迭代,但我无法计算有多少人添加了第4个月作为他们的inputHello,感谢大家的回答。我尝试过几种方法,比如for循环和通过arraylist进行迭代,但我不知道如何正确编写代码。我是否应该制作第二个arraylist来存储我在arraylist中找到的月数,然后打印出添加了多少个月这是我到目前为止的代码。迭代器it=club.Iterator;whileit.hasNext{Membership Membership=membershit.next;ifjoined==Membership.getMonth count++;{ArrayList nmonth=new ArrayList;nmonth.addcount;int sum=0;forIterator i=nmonth.iterator;i.hasNext;{sum+=count;返回sum;}如何将计数添加到新的arraylist中,然后求和并打印出总数?@Lynch感谢您的回答。是否有一些方法可以不使用哈希表来完成此任务?@Kare是的,您可以使用列表列表:list PersonLists=new arraylist;PersonLists.addnew arraylist;。您必须在顶级列表中添加12个新列表。然后您可以使用以下命令填充列表中的元素:personsLists.getperson.month-1.addperson。@Lynch我到目前为止所拥有的。您能告诉我如何将指定的数字从ArrayList 1添加到ArrayList 2中,然后打印出来吗?public int joinedMontHint month{int joined=month;if joined<1 | | joined>12{System.out.println不是有效月份;}否则{int count=0;//int countMount Iterator it=club.Iterator;whileit.hasNext{Membership Membership=membershit.next;ifjoined==Membership.getMonth count++;@Kare您所做的似乎是正确的。您希望得到什么?所有计数的总和?还是只希望输出显示月份id,然后显示与之关联的计数?是的,所选月份的所有计数的总和。如何计算将计数值添加到新的arraylist或数组中,然后打印总和?