如何打印for循环HashMap结果一次?

时间:2021-06-23 02:04:20

This is a method to print out the fee records for a particular member but it keeps on printing out the result as many as the number of members.

这是一种打印特定成员的费用记录的方法,但它会继续打印出与成员数量一样多的结果。

public void printFeeRecords(String anID)
{
    Member aMembers = findMemberByID(anID);


    for( Member aMember : members){

        System.out.println(aMembers.getFeeRecords());

    }
}

3 个解决方案

#1


1  

You already found the correct member - you don't need to iterate the others:

您已经找到了正确的成员 - 您不需要迭代其他成员:

public void printFeeRecords(String anID)
{
    Member aMembers = findMemberByID(anID);
    System.out.println(aMembers.getFeeRecords());
}

#2


1  

By writing:

1. for( Member aMember : members) {
2.    System.out.println(aMembers.getFeeRecords());
3. }

What you're saying is:

你的意思是:

  1. For each thing in members, bind that thing to the variable aMember, and do line 2

    对于成员中的每个事物,将该事物绑定到变量aMember,然后执行第2行

  2. Take the thing bound to aMembers, and call its getFeeRecords() method, and print the result

    将绑定到aMembers的东西,并调用其getFeeRecords()方法,并打印结果

So while we're using the correct member (aMembers - with an 's', not aMember, the loop variable), we're still going to do the thing once for each thing in members.

因此,当我们使用正确的成员(aMembers - 带有's',而不是成员,循环变量)时,我们仍然会为成员中的每件事做一次。

Note that if the loop was for( Member aMembers : members)... we would be printing the fee record for each thing in members i.e. the original value of aMembers set beforehand from findMemberByID() would be overwritten while we were in the loop. After the loop ends, though, we would still have it. This is related to a concept called scope

请注意,如果循环是针对(成员aMembers:成员)...我们将打印成员中每件事的费用记录,即在我们进入循环时,将从findMemberByID()预先设置的aMembers的原始值被覆盖。但是,在循环结束后,我们仍然会拥有它。这与称为范围的概念有关

#3


0  

You practically ask if the current member has the id provided in the parameter - anID.

您几乎会询问当前成员是否在参数 - anID中提供了id。

   public void printFeeRecords(String anID) {

        for( Member aMember : members) {

            if (aMember.getId() == anId) {

                System.out.println(aMembers.getFeeRecords());
            }
        }
    }

#1


1  

You already found the correct member - you don't need to iterate the others:

您已经找到了正确的成员 - 您不需要迭代其他成员:

public void printFeeRecords(String anID)
{
    Member aMembers = findMemberByID(anID);
    System.out.println(aMembers.getFeeRecords());
}

#2


1  

By writing:

1. for( Member aMember : members) {
2.    System.out.println(aMembers.getFeeRecords());
3. }

What you're saying is:

你的意思是:

  1. For each thing in members, bind that thing to the variable aMember, and do line 2

    对于成员中的每个事物,将该事物绑定到变量aMember,然后执行第2行

  2. Take the thing bound to aMembers, and call its getFeeRecords() method, and print the result

    将绑定到aMembers的东西,并调用其getFeeRecords()方法,并打印结果

So while we're using the correct member (aMembers - with an 's', not aMember, the loop variable), we're still going to do the thing once for each thing in members.

因此,当我们使用正确的成员(aMembers - 带有's',而不是成员,循环变量)时,我们仍然会为成员中的每件事做一次。

Note that if the loop was for( Member aMembers : members)... we would be printing the fee record for each thing in members i.e. the original value of aMembers set beforehand from findMemberByID() would be overwritten while we were in the loop. After the loop ends, though, we would still have it. This is related to a concept called scope

请注意,如果循环是针对(成员aMembers:成员)...我们将打印成员中每件事的费用记录,即在我们进入循环时,将从findMemberByID()预先设置的aMembers的原始值被覆盖。但是,在循环结束后,我们仍然会拥有它。这与称为范围的概念有关

#3


0  

You practically ask if the current member has the id provided in the parameter - anID.

您几乎会询问当前成员是否在参数 - anID中提供了id。

   public void printFeeRecords(String anID) {

        for( Member aMember : members) {

            if (aMember.getId() == anId) {

                System.out.println(aMembers.getFeeRecords());
            }
        }
    }