如何比较数组列表中的两个名称
我试图比较婴儿阵列列表中的两个名称,如果它是真的,它将返回“阵列中的两个婴儿是相同的”,如果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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码有很多问题。
42
,那么在这种情况下,您始终会得到false
。老实说,我不知道您在这里要做什么。我认为这是一项家庭作业,您必须证明自己的循环编程技能。鉴于此假设,我建议您采用以下解决方案:
There are many problems with your code.
42
then you always getfalse
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:
可以将列表转换为集合(集合只能包含唯一值),并比较2列表和集合的长度,如果列表不同,则列表包含一个双值,如果是相同的,则仅包含唯一的项目。
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.