Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关键字搜索并行数组_Java - Fatal编程技术网

Java 关键字搜索并行数组

Java 关键字搜索并行数组,java,Java,你好,我正在尝试写代码到关键字搜索parralele数组我有2个数组 String[] beerNames = new String[10]; //define berrNames array Double[] beerStrengths = new Double[10]; //define beerStrengths array beerNames[0] = "Heineken"; //fill the arrays beerNames[1]

你好,我正在尝试写代码到关键字搜索parralele数组我有2个数组

    String[] beerNames = new String[10]; //define berrNames array
    Double[] beerStrengths = new Double[10]; //define beerStrengths array        

    beerNames[0] = "Heineken"; //fill the arrays
    beerNames[1] = "Bud Light";
    beerNames[2] = "Coors Light";
    beerNames[3] = "Leffe Blonde";
    beerNames[4] = "Budweiser";
    beerNames[5] = "Erdinger Non-Alcoholic";
    beerNames[6] = "Bud Premier Select";
    beerNames[7] = "Corona";
    beerNames[8] = "Barefoot Bohemian";
    beerNames[9] = "3 Monts";

    beerStrengths[0] = 4.0;
    beerStrengths[1] = 4.2;
    beerStrengths[2] = 4.3;
    beerStrengths[3] = 6.6;
    beerStrengths[4] = 5.0;
    beerStrengths[5] = 0.0;
    beerStrengths[6] = 7.4;
    beerStrengths[7] = 4.6;
    beerStrengths[8] = 4.0;
    beerStrengths[9] = 8.5; //fill the arrays
我想能够搜索关键字,所以如果我输入巴德,我会得到一个搜索结果

“Bud Light”的酒精含量为4.2% “百威”的酒精含量为5.0%

可悲的是,我不知道如何做到这一点,我一直在寻找一些代码,我从搜索一个文件 扫描仪控制台=新的ScannerSystem.in; System.out.printSearch短语:; 字符串搜索=console.nextLine.toLowerCase

    Scanner input = new Scanner(new File("imdb.txt"));
    // 1 9.1 243153 The Godfather (1972)

    while (input.hasNextLine()) {
        String line = input.nextLine();
        if (line.toLowerCase().contains(search)) {
            //System.out.println(line);
            displayMovieInfo(line);
        }
    }
}

public static void displayMovieInfo(String line) {
    Scanner lineScan = new Scanner(line);
    int rank = lineScan.nextInt();
    double rating = lineScan.nextDouble();
    lineScan.nextInt();

    String title = "";
    while (lineScan.hasNext()) {
        title = title + lineScan.next() + " ";
    }
    System.out.println(rank + "\t" + rating + "\t" + title);
}

但是我不知道如何转换这段代码。如果您知道第一个数组的数组索引,您就知道第二个数组的索引,我们将不胜感激。这听起来也像是创建具有两个属性的对象的候选者:

public class BeerStrength  {
   private final String name;
   private final int strengthNum;
   public BeerStrength(String name, int strength_num)  {
      this.name = name;
      strengthNum = strength_num;
   }
   public String getName()  {
      return  name;
   }
   public int getStrength()  {
      return  strengthNum;
   }
}

然后你有一个BeerStrength数组,这将使你的并行数组问题变得毫无意义。

使用HashMap代替两个数组。使用String作为键,Double作为值。这样,您就可以迭代键并返回值。您将大大减少代码,并且只使用一种数据结构。

我要做的第一件事是改变您的方法。如果您发现自己在处理多个阵列,那么通常可以采用不同的方法。例如,您可以创建一个以名称和体积为值的Beer类

public class Beer
{
    private String name;
    private double volume;

    public Beer(String name, double volume)
    {
        this.name = name;
        this.volume = volume;
    }

    public boolean equals(Object o) {
        if(o instanceof Beer) {
           Beer beer = (Beer)o;

           if(this.name.equals(beer.getName())) {
               return true;
           }
        }

        return false;
    }

    public String getName() {return this.name; }
    public double getVolume() {return this.volume; }
}
然后你可以创建一堆啤酒对象,把它们放在一个列表中

在填充时,您会执行以下操作

beers = new ArrayList<Beer>();

beers.add(new Beer("Budweiser", 5));
// Etc..

现在,您可以自由地进行输入,并了解如何搜索:

我认为我们的两个答案合在一起才是真正的答案。啤酒名称的关键,价值啤酒的力量。加上@christopher's equals函数,我们就有了一个古老的答案——冰沙。伟大的头脑会想一个类似的东西,不是吗!三个发霉的舵手再次罢工。“克里斯:如果这个答案帮助了你,请考虑投票,并接受它作为你的答案,点击大绿勾选。”祝你好运,欢迎来到stackoverflow!
beers = new ArrayList<Beer>();

beers.add(new Beer("Budweiser", 5));
// Etc..