gpt4 book ai didi

java - 返回null是对的吗?

转载 作者:行者123 更新时间:2023-11-30 06:58:11 25 4
gpt4 key购买 nike

<分区>

返回null是否正确,如果在数组中找不到对象,我认为它没有用,或者如何做正确?

public Product FindByProductName(Product [] products,String productName){

for(Product item:products)
if(item.GetProductName().equals(productName))
return item;
return null;
}

没有用,因为我必须像这样使用该功能

Product result = products.FindByProductName(productsArray, "someName");

if(result == null)
System.out.print("Object not found!");
else result.Show();

public void Show() {
System.out.println("Название продукта = " + this.GetProductName() + ";" + " Код продукта = " + this.GetProductCode() + ";" + " Марка металла = " + this.GetBrandMetal() + ";" + " Вес продукта = " + this.GetWeightOfProduct() + ";");
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com