简体   繁体   English

如何使用二进制搜索来查找具有一定权重的数组中的第一个元素(另一个数组中的另一个元素)?

[英]How to use binary search in order to find the first element in an array that has certain weight (another element in a different array)?

All the methods here are correct, but my problem is that i have to find a part in the parts array that has a certain weight. 这里的所有方法都是正确的,但是我的问题是我必须在零件阵列中找到具有一定权重的零件。 So After i do the getweight method I think i have to call that. 所以,在我执行getweight方法之后,我想我必须调用它。 But the last part of the code is what i have a problem with. 但是代码的最后一部分是我遇到的问题。 It starts with the line public Part getPartWithWeight (int weight){ 它从以下行开始:Public Part getPartWithWeight(int weight){

class Robot {
             Part[] parts;
                 public Robot () {// assume these are right}
                 }
                 public void addPart(Part p) { // assume these are right}
                 }

             class Part {
             // Class details not shown
                 public double getWeight() {//... }
                 }
                 public int getPartnum() {//...}
                 }
                 public String getMaterial() {//...}     
                 }

             public Part getPartWithWeight (int weight){
             for(int i = 0; i < parts.length; i ++){
                 if (parts[i].weight == weight) {
                     return parts[i];
                 }
                 }

Well, here's the "works-out-of-the-box" version. 好吧,这是“即开即用”的版本。

Step 1 Write a comparator. 步骤1编写一个比较器。

class PartComparator implements java.util.Comparator<Part> {
    @Override
    public int compare(Part part1, Part part2) {
        return part1.weight - part2.weight;
    }
    public final static PartComparator instance = new PartComparator();
}

The comparator class should be static if you declare it inside Part (which is what I would suggest). 如果在Part内声明比较器类,则它应该是静态的(这是我建议的)。

Step 2 Use the comparator 步骤2使用比较器

public Part getPartWithWeight (int weight){
    Part pivot = new Part();
    pivot.weight = weight;
    int idx = Arrays.binarySearch(parts, pivot, PartComparator.instance);
    return parts[idx];
}
if (parts[i].weight == weight)

should say 应该说

if (parts[i].getWeight() == weight)

You should use getWeight() because that's the method defined in the Parts class. 您应该使用getWeight(),因为那是Parts类中定义的方法。 Also if you're problem is getting the parts array from the robot class and using it in the part class then you should just pass it as a parameter. 另外,如果您有问题,请从机械手类中获取零件数组并在零件类中使用它,那么只需将其作为参数传递即可。

public Part getPartWithWeight (int weight, Part[] parts){
  for(int i = 0; i < parts.length; i ++){
    if (parts[i].getWeight() == weight) {
      return parts[i];
    }
  }
}

Then when you call the method getPartWithWeight() make sure you put the array in there too. 然后,当您调用方法getPartWithWeight()时,请确保也将数组放入其中。

EDIT: Also per Ray Cheng's comment, the weight you're looking for is supposed to be an int but your getWeight() method returns a double.... so either your getWeight() should return an in or you should typecast it as an int before making the comparison. 编辑:同样根据Ray Cheng的评论,您正在寻找的权重应该是一个整数,但是您的getWeight()方法返回一个double ....,因此您的getWeight()应该返回in或您应该将其键入为进行比较之前的int值。 Be aware that if you typecast it as an int you will lose some precision. 请注意,如果将其作为int进行转换,则将失去一些精度。 It would be better to fix your getWeight() so it returns a double like you need it to. 最好修复getWeight(),以便它返回所需的double值。

To implement a binary search, you first need to cut the array in half and then decide which half you want to begin the search. 要实现二进制搜索,您首先需要将数组切成两半,然后确定要开始搜索的是哪一半。 Then you cut the halved array in half again and repeat... 然后您将切成两半的数组再切成两半,然后重复...

Unless you want to implement your own, but looks like there is a built-in one you can call. 除非您想实现自己的实现,但是看起来好像有一个内置的可以调用。

http://docs.oracle.com/javase/7/docs/api/index.html?java/util/Arrays.html http://docs.oracle.com/javase/7/docs/api/index.html?java/util/Arrays.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM