0

我有一个字符串数组,称为食物,
其中

food[0] = "banana fruit"
food[1] = "potato vegetable"
food[2] = "peas vegetable"
food[3] = "apple fruit"
food[4] = "pumpkin vegetable"

我如何只打印蔬菜?

4

1 回答 1

3

您可以使用其中一个endsWith()contains()String 方法:

for (String s : food) {
  if (s.endsWith("vegetable"))
    System.out.println(s);
}
于 2013-06-08T05:30:11.913 回答