找出ArrayList中是否包含某个char或部分字符串,然后打印结果

时间:2014-10-10 18:14:22

标签: java arraylist

流动的代码不会显示它是否只包含一个字符,例如花名称中的' a'

private static void filterFlower (ArrayList<Flower> flower) {

    String floName = getString("Enter the partial name of the flower: ");

    boolean flr = flower.contains(floName);

    if (flr == true) {
        System.out.println("\nThe Flowers are filtered by " + floName + "\n");
        System.out.println(floName);
    }
    else
    {
        System.out.println("There isn't a flower with the following: "+ floName + " " + "\n");
    }
}

3 个答案:

答案 0 :(得分:1)

假设你的Flower类有一个name成员和一个吸气剂:

private static void filterFlower (ArrayList<Flower> flowers) {

    String partialName = getString("Enter the partial name of the flower: ");

    ArrayList<Flower> filteredFlowers = new ArrayList<Flower>();
    for(Flower flower: flowers){
        if (flower.getName().contains(partialName)){
            filteredFlowers.add(flower);
        }
    }
    if (filteredFlowers.size() > 0){
        System.out.println("The flowers containing " + partialName + " are: ");
        for(Flower flower: filteredFlowers){
            System.out.println(flower.getName());
        }
    }
    else {
        System.out.println("None of the flowers contain " + partialName);
    }
}

答案 1 :(得分:0)

这样的事可能有用(未经测试):

 boolean contains = false;
 String floName = getString("Enter the partial name of the flower: ");
 for(Flower f : flower) {
     contains |= f.getName().contains(floName);
     if (contains) {
        System.out.println("\nThe Flowers are filtered by " + floName + "\n");
        System.out.println(floName);
        return;
     }
 }
 System.out.println("There isn't a flower with the following: "+ floName);

答案 2 :(得分:0)

由于你有一系列鲜花,你需要循环收集并比较每个项目。您选择做什么取决于您希望应用程序完成的任务。

此代码与您的代码略有不同,但您会明白这一点。

public static void main(String[] args){
    ArrayList<Flower> flowers = new ArrayList<Flower>();
    flowers.add(new Flower("Amaranth"));
    flowers.add(new Flower("Anemone"));
    flowers.add(new Flower("Amaryllis"));
    filterFlower(flowers);
}

此外,由于您的参数是一个集合,因此将它重命名为花是有意义的,因为它更容易看到它实际上是什么(不读取类型声明)。

private static void filterFlower (ArrayList<Flower> flowers) {
    System.out.print("Enter the partial name of the flower: ");

    Scanner scanner = new Scanner(System.in);
    String partial = scanner.next();

    ArrayList<Flower> matchedFlowers = new ArrayList<Flower>();
    for (Flower flower : flowers){
        if(flower.contains(partial))
            matchedFlowers.add(flower);
    }

    if (matchedFlowers.isEmpty()) {
        System.out.println("No match for '" + partial + "'");
    }
    else
    {
        System.out.println("Matches for '" + partial + "':");
        for(Flower flower : matchedFlowers){
            System.out.println(flower.name);
        }
    }
}

上面的代码将起作用,因为您还定义了Flower如下:

public class Flower {
   public String name;
   public Flower(String name){
      this.name = name;
   }
   public boolean contains(String partial){
      return name.contains(partial);
   }
}

这段代码肯定可以更清晰地编写,并针对此进行了优化。你甚至可以用许多不同的方式重写它(即使它是一个简单的应用程序)。我仍然认为它回答了你的问题。