如果对象名不在列表中,是否将类对象添加到列表中?

nafvub8i  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(335)

我看了其他问题,但似乎找不到我要找的答案。
我很难弄清楚如何创建一个将类对象添加到 ArrayList 只有在列表中没有使用它的名称时。
这是我上的课。

package myPackage;

public class Cube {
    private int length;
    private String name;

    public Cube(int initLength, String initName) {
        this.length = initLength;
        this.name = initName;
    }

我想创建新的多维数据集并将它们添加到列表中。这是我尝试使用的代码。
在while循环中,我不知道如何确定是否使用了这个名称

package myPackage;

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

public class PartFive {

    public static void main(String[] args) {
        ArrayList<Cube> cubelist = new ArrayList<>();
        Cube oshea = new Cube (13, "oshea");
        Cube mike = new Cube (7, "tony");
        cubelist.add(oshea);
        cubelist.add(mike);

        Scanner reader = new Scanner(System.in);
        while (true) {
            System.out.println("enter cube name (blank quits): ");
            String name = reader.nextLine();
            if (name.equals("")){
                break;
            }
            System.out.println("enter side length: ");
            int length = Integer.valueOf(reader.nextLine());

            Cube newcube = new Cube(length, name);
            if(cubelist.contains(newcube.name)) {
                // dont add to list
            }
            else {
                cubelist.add(newcube);
            }
        }
        reader.close();
        System.out.println(cubelist);
    }
}

欢迎任何建设性的批评和建议。

2sbarzqh

2sbarzqh1#

可以使用lambda表达式检查cubelist数组中的重复名称(为了更好的可读性):

boolean isNameAlreadyExisting = cubelist.stream()
                .anyMatch(cube -> cube.getName().equals(newcube.getName())); // this is returning true if any of the cubelist element's name is equal with the newcube's name, meaning that the name is already existing in the cubelist 

 if (!isNameAlreadyExisting) {
   cubelist.add(newcube);
 }

您应该做的一件事是删除while(true)指令,它会导致无限循环。另一个建议是显示cubelist包含的对象的名称,以确保名称不重复:

cubelist.stream()
        .map(Cube::getName)
        .forEach(System.out::println);
ldxq2e6h

ldxq2e6h2#

根据你问题中的代码:

if(cubelist.contains(newcube.name)) {
    // don't add to list
}
else {
    cubelist.add(newcube);
}

方法在类中包含 java.utilArrayList 是一条路要走,但你需要知道的方法 contains [最终]调用其元素类型的方法equals。在您的例子中,元素类型是 Cube . 因此您需要添加 equals 方法到类 Cube . 我不知道是什么决定了两个 Cube 物体是相等的,但我猜,根据你的问题,如果它们有相同的 name ,即使他们有不同的 length s。我将进一步假设 name 不能为空。基于这些假设,这里有一个 equals 方法。您应该将此方法添加到类中 Cube .

public boolean equals(Object obj) {
    boolean areEqual = false;
    if (this == obj) {
        areEqual = true;
    }
    else {
        if (obj instanceof Cube) {
            Cube other = (Cube) obj;
            areEqual = name.equals(other.name);
        }
    }
    return areEqual;
}

现在,在方法上 main 班级 PartFive 您可以使用以下方法 if 添加 Cube 加入名单。

if (!cubelist.contains(newcube)) {
    cubelist.add(newcube);
}
p1iqtdky

p1iqtdky3#

代替

if(cubelist.contains(newcube.name)) {
  dont add to list
}
else {
  cubelist.add(newcube);
}

具有

boolean found = false;
for(Cube cube: cubelist){
    if(cube.getName().equals(name)) {
        found = true;
        break;
    }
}

if(!found) {
    cubelist.add(newcube);
}

我们的想法是使用 boolean 用于跟踪与输入具有相同名称的多维数据集的变量 name 列表中已存在。为此,迭代 cubelist 如果一个立方体与输入的名称相同 name 发现时,更改 boolean 变量和 break 循环。如果 boolean 变量不会在整个循环中更改,请将多维数据集添加到列表中。

相关问题