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

发布于 2025-01-05 02:30:22 字数 908 浏览 1 评论 0原文

这里的所有方法都是正确的,但我的问题是我必须在零件数组中找到具有一定权重的零件。所以在我执行 getweight 方法之后,我想我必须调用它。但代码的最后一部分是我遇到的问题。它以行 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];
                 }
                 }

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. But the last part of the code is what i have a problem with. It starts with the line 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];
                 }
                 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜宝宝 2025-01-12 02:30:22

嗯,这是“开箱即用”版本。

第 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();
}

如果您在 Part 中声明它,则比较器类应该是静态的(这是我的建议)。

第 2 步 使用比较器

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

Well, here's the "works-out-of-the-box" version.

Step 1 Write a comparator.

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).

Step 2 Use the comparator

public Part getPartWithWeight (int weight){
    Part pivot = new Part();
    pivot.weight = weight;
    int idx = Arrays.binarySearch(parts, pivot, PartComparator.instance);
    return parts[idx];
}
玻璃人 2025-01-12 02:30:22
if (parts[i].weight == weight)

应该说

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

您应该使用 getWeight() 因为这是 Parts 类中定义的方法。
另外,如果您的问题是从机器人类获取零件数组并在零件类中使用它,那么您应该将其作为参数传递。

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

然后,当您调用 getPartWithWeight() 方法时,请确保将数组也放入其中。

编辑:另外,根据 Ray Cheng 的评论,您要查找的权重应该是 int 但您的 getWeight() 方法返回一个 double...所以您的 getWeight() 应该返回一个 in 或者您应该将其类型转换为在进行比较之前先输入一个 int 。请注意,如果将其类型转换为 int,您将失去一些精度。最好修复您的 getWeight() ,以便它返回您需要的双精度值。

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.
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.

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. Be aware that if you typecast it as an int you will lose some precision. It would be better to fix your getWeight() so it returns a double like you need it to.

梦亿 2025-01-12 02:30:22

要实现二分搜索,您首先需要将数组切成两半,然后决定要开始搜索的一半。然后,您再次将减半的数组切成两半并重复...

除非您想实现自己的数组,但看起来有一个可以调用的内置数组。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文