英文:
Why doesn't this method give any output?
问题
我有一个对象的ArrayList,并且我想在该ArrayList中的特定字段中搜索关键字。我有一个名为Product的类,一个名为products的ArrayList,我尝试了以下代码:
public static void Search(String keywords) {
    for (int i = 0; i < products.size(); i++) {
        if (products.get(i).getTitle().toLowerCase().contains(keywords.toLowerCase())) {
            System.out.println(products.get(i).toString());
        }
    }
}
"Keywords"是要搜索的字符串。
Product类如下:
public class Product {
    String title;
    ImageIcon image;
    public String getTitle() {
        return title;
    }
    public Product(String title) {
        this.title = title;
    }
    @Override
    public String toString() {
        return "Product{" + "title=" + title + '}';
    }
}
我还创建了Product的一些实例,放在了products的ArrayList中:
ArrayList<Product> products = new ArrayList<>();
products.add(new Product("Car"));
products.add(new Product("House"));
products.add(new Product("Phone"));
然后我调用了这个函数:
Search("car");
当我调用"Search"方法时,没有输出被打印出来。为什么?谢谢!
编辑:toString方法已包含在Product类中。
英文:
I have an ArrayList of objects, and I want to search in that ArrayList keywords, but searching in a specific field of the class. I have the class Product, the ArrayList products and I tried this:
public static void Search (String keywords){
    for (int i = 0; i < products.size(); i++){
        if (products.get(i).getTitle().toLowerCase().contains(keywords.toLowerCase())){
            System.out.println(products.get(i).toString());
        }
    }
}
"Keywords" is a string to search for.
The class Product is this:
 public class Product {
    String title;
    ImageIcon image;
    public String getTitle() {
        return title;
    }
    public Product(String title) {
        this.title = title;
    }
   @Override
   public String toString() {
      return "Product{" + "title=" + title + '}';
   }
I also instanciated some objets of Product, inside a products ArrayList:
ArrayList<Product> products = new ArrayList<>();
products.add(new Product("Car"));
products.add(new Product("House"));
products.add(new Product("Phone"));
And I call the function:
    Search("car");
When I call the method "Search", no output is printed. Why? Thanks!
EDIT: the "toString" method is included in the Product class.
答案1
得分: 1
- 如果你没有得到任何输出,意味着
contains返回了false。 - 更清晰的方式是使用增强的
for循环。 - 你应该遵循Java命名约定,例如
Search应该根据命名约定改为search。 - 在对变量执行任何操作之前,应该检查它们是否为
null。 - 在
System.out.println内部不需要显式调用toString(),因为它会被隐式调用。 
public static void search(String keyword) {
	for (Product product : products) {
		if (keyword != null && product != null && product.getTitle() != null
				&& product.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
			System.out.println(product);
		}
	}
}
演示:
import java.util.ArrayList;
class Product {
	String title;
	public String getTitle() {
		return title;
	}
	public Product(String title) {
		this.title = title;
	}
	@Override
	public String toString() {
		return "Product{" + "title=" + title + '}';
	}
}
public class Main {
	public static void main(String[] args) {
		ArrayList<Product> products = new ArrayList<>();
		products.add(new Product("Car"));
		products.add(new Product("House"));
		products.add(new Product("Phone"));
		search("car", products);
	}
	public static void search(String keyword, ArrayList<Product> products) {
		for (Product product : products) {
			if (keyword != null && product != null && product.getTitle() != null
					&& product.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
				System.out.println(product);
			}
		}
	}
}
输出:
Product{title=Car}
英文:
- If you are not getting any output, it means 
containsreturnsfalse. - A cleaner way is to use enhanced 
forloop. - You should follow Java naming conventions e.g. 
Searchshould besearchas per the naming convention. - You should check the variables for 
nullbefore performing any operation on them. - You do not need to call 
toString()explicitly insideSystem.out.printlnas it is implicitly called. 
public static void search(String keyword) {
	for (Product product : products) {
		if (keyword != null && product != null && product.getTitle() != null
				&& product.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
			System.out.println(product);
		}
	}
}
Demo:
import java.util.ArrayList;
class Product {
	String title;
	public String getTitle() {
		return title;
	}
	public Product(String title) {
		this.title = title;
	}
	@Override
	public String toString() {
		return "Product{" + "title=" + title + '}';
	}
}
public class Main {
	public static void main(String[] args) {
		ArrayList<Product> products = new ArrayList<>();
		products.add(new Product("Car"));
		products.add(new Product("House"));
		products.add(new Product("Phone"));
		search("car", products);
	}
	public static void search(String keyword, ArrayList<Product> products) {
		for (Product product : products) {
			if (keyword != null && product != null && product.getTitle() != null
					&& product.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
				System.out.println(product);
			}
		}
	}
}
Output:
Product{title=Car}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论