如何使用二分搜索来查找数组中具有一定权重的第一个元素(不同数组中的另一个元素)?
这里的所有方法都是正确的,但我的问题是我必须在零件数组中找到具有一定权重的零件。所以在我执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这是“开箱即用”版本。
第 1 步编写一个比较器。
如果您在 Part 中声明它,则比较器类应该是静态的(这是我的建议)。
第 2 步 使用比较器
Well, here's the "works-out-of-the-box" version.
Step 1 Write a comparator.
The comparator class should be static if you declare it inside Part (which is what I would suggest).
Step 2 Use the comparator
应该说
您应该使用 getWeight() 因为这是 Parts 类中定义的方法。
另外,如果您的问题是从机器人类获取零件数组并在零件类中使用它,那么您应该将其作为参数传递。
然后,当您调用 getPartWithWeight() 方法时,请确保将数组也放入其中。
编辑:另外,根据 Ray Cheng 的评论,您要查找的权重应该是 int 但您的 getWeight() 方法返回一个 double...所以您的 getWeight() 应该返回一个 in 或者您应该将其类型转换为在进行比较之前先输入一个 int 。请注意,如果将其类型转换为 int,您将失去一些精度。最好修复您的 getWeight() ,以便它返回您需要的双精度值。
should say
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.
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.
要实现二分搜索,您首先需要将数组切成两半,然后决定要开始搜索的一半。然后,您再次将减半的数组切成两半并重复...
除非您想实现自己的数组,但看起来有一个可以调用的内置数组。
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