如何比较数组列表中的两个名称

发布于 2025-02-06 17:01:33 字数 2536 浏览 2 评论 0原文

我试图比较婴儿阵列列表中的两个名称,如果它是真的,它将返回“阵列中的两个婴儿是相同的”,如果false会返回“阵列中没有同一个婴儿”。 但是我只是无法为FindTwoSameBabies方法工作,它应该比较数组中的所有名称并返回结果。

import java.util.ArrayList;
import java.util.Scanner;

public class BabyClient {

private static Baby[] babies;

public static void main(String[] args) {
    // Initialize ArrayList of Baby
    ArrayList<Baby> babiesList = new ArrayList<>();
    // Object of Scanner class to take input
    Scanner input = new Scanner(System.in);
    // Populating babies array list
    for (int i = 0; i < 4; i++) {
    // a.
        getBabyDetails(babiesList, input);
    }
    // b.
    printBabyDetails(babiesList);
    
    // c.
    System.out.println("\nThe average age of all babies in the array is " + calculateAndPrintBabiesAverageAge(babiesList));
    
    // d.
    findTwoSameBabies(babiesList);

    
}

private static void getBabyDetails(ArrayList<Baby> babies, Scanner input) {
    // get name and age of the baby
System.out.print("Enter name of baby: ");
String name = input.next();
System.out.print("Enter age of baby: ");
int age = input.nextInt();
input.nextLine();
// create an baby instance
Baby baby = new Baby(name, age);
// add baby to the list
babies.add(baby);

}

private static void findTwoSameBabies(ArrayList<Baby> babiesList) {
    boolean sameBaby = false;
    for (Baby babie : babiesList) {
        for(int i=0;i<babiesList.size();i++) {
            for(int j=i+1;j<babiesList.size();j++) {
                if(babie.getName().equalsIgnoreCase(babiesList.size())) {
                    sameBaby = true;
                    break;//break from inner for loop j
                }
            }
            if(sameBaby) {
                break;//break from outer for loop i
            }
        }
    }
    
    if(sameBaby) {
        System.out.println("Two babies in the array are the same");
    }else {
        System.out.println("No same baby in the array");
    }
}


private static double calculateAndPrintBabiesAverageAge(ArrayList<Baby> babies) {
    int totalAge = 0;
    for (Baby babie : babies) {
        totalAge += babie.getAge();
    }
    double averageAge = totalAge / babies.size();
    return averageAge;
    }

private static void printBabyDetails(ArrayList<Baby> babiesList) {
    for(int i=0;i<babiesList.size();i++) {
        System.out.println("Baby "+(i+1)+ ": " + babiesList.get(i).getName() + ", " + "Age: " + babiesList.get(i).getAge());
        System.out.println();
        
    }
}

}

I am trying to compare the two names in the baby array list if it is true it will return "Two babies in the array are the same" if false will return "No same baby in the array".
But I just can't get it to work for the findTwoSameBabies method, it is supposed to compare all the names in the array and return the result.

import java.util.ArrayList;
import java.util.Scanner;

public class BabyClient {

private static Baby[] babies;

public static void main(String[] args) {
    // Initialize ArrayList of Baby
    ArrayList<Baby> babiesList = new ArrayList<>();
    // Object of Scanner class to take input
    Scanner input = new Scanner(System.in);
    // Populating babies array list
    for (int i = 0; i < 4; i++) {
    // a.
        getBabyDetails(babiesList, input);
    }
    // b.
    printBabyDetails(babiesList);
    
    // c.
    System.out.println("\nThe average age of all babies in the array is " + calculateAndPrintBabiesAverageAge(babiesList));
    
    // d.
    findTwoSameBabies(babiesList);

    
}

private static void getBabyDetails(ArrayList<Baby> babies, Scanner input) {
    // get name and age of the baby
System.out.print("Enter name of baby: ");
String name = input.next();
System.out.print("Enter age of baby: ");
int age = input.nextInt();
input.nextLine();
// create an baby instance
Baby baby = new Baby(name, age);
// add baby to the list
babies.add(baby);

}

private static void findTwoSameBabies(ArrayList<Baby> babiesList) {
    boolean sameBaby = false;
    for (Baby babie : babiesList) {
        for(int i=0;i<babiesList.size();i++) {
            for(int j=i+1;j<babiesList.size();j++) {
                if(babie.getName().equalsIgnoreCase(babiesList.size())) {
                    sameBaby = true;
                    break;//break from inner for loop j
                }
            }
            if(sameBaby) {
                break;//break from outer for loop i
            }
        }
    }
    
    if(sameBaby) {
        System.out.println("Two babies in the array are the same");
    }else {
        System.out.println("No same baby in the array");
    }
}


private static double calculateAndPrintBabiesAverageAge(ArrayList<Baby> babies) {
    int totalAge = 0;
    for (Baby babie : babies) {
        totalAge += babie.getAge();
    }
    double averageAge = totalAge / babies.size();
    return averageAge;
    }

private static void printBabyDetails(ArrayList<Baby> babiesList) {
    for(int i=0;i<babiesList.size();i++) {
        System.out.println("Baby "+(i+1)+ ": " + babiesList.get(i).getName() + ", " + "Age: " + babiesList.get(i).getAge());
        System.out.println();
        
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2025-02-13 17:01:33

您的代码有很多问题。

  • 您有一个迭代列表的最外部循环,这很好,
  • 您在此循环内有一个循环,该循环用索引
  • 和另一个相似的循环迭代,该循环与此元素中的一个元素一起进行,
  • 但是您比较了一个名称婴儿首先使用阵列的大小。这是一个数字,如果您的宝宝被称为42,那么在这种情况下,您始终会得到false

老实说,我不知道您在这里要做什么。我认为这是一项家庭作业,您必须证明自己的循环编程技能。鉴于此假设,我建议您采用以下解决方案:

private static void findTwoSameBabies(ArrayList<Baby> babiesList) {
    boolean sameBaby = false;
    for (Baby baby1 : babiesList) {
        for(int i=0;i<babiesList.size();i++) {
            Baby baby2 = babiesList.get(i);

            if(baby1.getName().equalsIgnoreCase(baby2.getName())) {
                sameBaby = true;
                break;//break from inner for loop i
            }           
        }

        if(sameBaby) { 
            break; // Break from the outer foreach loop 
        }
    }
    
    if(sameBaby) {
        System.out.println("Two babies in the array are the same");
    } else {
        System.out.println("No same baby in the array");
    }
}
  • 我们在外循环中的列表元素上迭代
  • ,并在内部循环中的同一列表中迭代
  • 我们将每个婴儿与其他每个婴儿进行比较,以检查重复项
  • 并打破两个循环,如果我们找到了同样的循环。名称

There are many problems with your code.

  • You have a most outer loop that iterates over the list, this is fine
  • You have a loop inside this loop that iterates over with an index
  • And an another, similar loop that goes ahead with one element from this element
  • But then you compare a name of the baby you get first with the size of the array. This is a number, and if your baby is not called as like 42 then you always get false in this case.

I honestly do not know what's your real job to do here. I assume this is a homework and you have to prove your loop programming skills. Given this assumption, I recommend you the following solution:

private static void findTwoSameBabies(ArrayList<Baby> babiesList) {
    boolean sameBaby = false;
    for (Baby baby1 : babiesList) {
        for(int i=0;i<babiesList.size();i++) {
            Baby baby2 = babiesList.get(i);

            if(baby1.getName().equalsIgnoreCase(baby2.getName())) {
                sameBaby = true;
                break;//break from inner for loop i
            }           
        }

        if(sameBaby) { 
            break; // Break from the outer foreach loop 
        }
    }
    
    if(sameBaby) {
        System.out.println("Two babies in the array are the same");
    } else {
        System.out.println("No same baby in the array");
    }
}
  • We iterate over the list elements in the outer loop
  • And iterate over the same list in the inner loop
  • We compare every baby with every other babies to check duplicates
  • And break both loops if we found same names
红衣飘飘貌似仙 2025-02-13 17:01:33

可以将列表转换为集合(集合只能包含唯一值),并比较2列表和集合的长度,如果列表不同,则列表包含一个双值,如果是相同的,则仅包含唯一的项目。

Set<Baby> babiesSet = new HashSet<>(babiesList);
if (babiesSet.size() != babiesList.size()) {
    return "Two babies in the array are the same";
} else {
    return "No same baby in the array";
}

It's possible to convert the list to a set (a set can only contain unique values) and compare the length of the 2 list and the set, if it's not the same the list contains a double value, if it's the same it only contains unique items.

Set<Baby> babiesSet = new HashSet<>(babiesList);
if (babiesSet.size() != babiesList.size()) {
    return "Two babies in the array are the same";
} else {
    return "No same baby in the array";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文