为什么这个方法没有输出?

huangapple go评论65阅读模式
英文:

Why doesn't this method give any output?

问题

我有一个对象的ArrayList,并且我想在该ArrayList中的特定字段中搜索关键字。我有一个名为Product的类,一个名为productsArrayList,我尝试了以下代码:

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的一些实例,放在了productsArrayList中:

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 &lt; 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 &quot;Product{&quot; + &quot;title=&quot; + title + &#39;}&#39;;
   }

I also instanciated some objets of Product, inside a products ArrayList:

ArrayList&lt;Product&gt; products = new ArrayList&lt;&gt;();

products.add(new Product(&quot;Car&quot;));
products.add(new Product(&quot;House&quot;));
products.add(new Product(&quot;Phone&quot;));

And I call the function:

    Search(&quot;car&quot;);

When I call the method "Search", no output is printed. Why? Thanks!

EDIT: the "toString" method is included in the Product class.

答案1

得分: 1

  1. 如果你没有得到任何输出,意味着contains返回了false
  2. 更清晰的方式是使用增强的for循环。
  3. 你应该遵循Java命名约定,例如Search应该根据命名约定改为search
  4. 在对变量执行任何操作之前,应该检查它们是否为null
  5. 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}
英文:
  1. If you are not getting any output, it means contains returns false.
  2. A cleaner way is to use enhanced for loop.
  3. You should follow Java naming conventions e.g. Search should be search as per the naming convention.
  4. You should check the variables for null before performing any operation on them.
  5. You do not need to call toString() explicitly inside System.out.println as it is implicitly called.
public static void search(String keyword) {
	for (Product product : products) {
		if (keyword != null &amp;&amp; product != null &amp;&amp; product.getTitle() != null
				&amp;&amp; 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 &quot;Product{&quot; + &quot;title=&quot; + title + &#39;}&#39;;
	}
}

public class Main {
	public static void main(String[] args) {
		ArrayList&lt;Product&gt; products = new ArrayList&lt;&gt;();
		products.add(new Product(&quot;Car&quot;));
		products.add(new Product(&quot;House&quot;));
		products.add(new Product(&quot;Phone&quot;));
		search(&quot;car&quot;, products);
	}

	public static void search(String keyword, ArrayList&lt;Product&gt; products) {
		for (Product product : products) {
			if (keyword != null &amp;&amp; product != null &amp;&amp; product.getTitle() != null
					&amp;&amp; product.getTitle().toLowerCase().contains(keyword.toLowerCase())) {
				System.out.println(product);
			}
		}
	}
}

Output:

Product{title=Car}

huangapple
  • 本文由 发表于 2020年4月11日 00:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61144085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定