如何计算给定数组的两个元素之间的数组元素的数量
我想编写一个方法,当提供一个整数数组时,该方法将执行以下操作。对于每对数组元素,它将组合它们并将它们放入内部类对象的列表中。然后它将比较数组中的每个元素并检查它是否适合每对值。 (即我有一个数组 0, 2, 4,它会生成例如对 (0,4),然后它会检查值 2 确实在 0 和 4 之间,因此计数器会增加)。我尝试了以下代码,但它返回 0。如何修复它或者是否有更简单的方法来实现此目的?我主要关心返回值是否正确。谢谢
import java.util.*;
import java.util.Map;
import java.lang.*;
public class Prac1 {
public int count(int[] A){
int k = 0;
class PTemp{
int first = -1;
int second = -1;
public PTemp(int first, int second){
int f = first;
int s = second;
}
}
List<PTemp> r = new ArrayList<PTemp>();
for (int i = 0; i < A.length; i++) {
for (int j = i+1; j < A.length; j++) {
r.add(new PTemp(A[i], A[j]));
r.add(new PTemp(A[j], A[i]));
//System.out.println("["+A[i] +","+A[j]+"]");
//System.out.println("["+A[j] +","+A[i]+"]");
}
}
Iterator<PTemp> ir = r.iterator();
while (ir.hasNext()){
PTemp p = ir.next();
for (int i = 0; i < A.length; i++){
if (((p.first < A[i]) && (A[i] < p.second)) || ((p.first > A[i]) && (A[i] > p.second))){
k = k + 1;
}
}
}
return k;
}
public static void main(String[] args){
int[] A = {0, 2, 4};
Prac1 pr = new Prac1();
System.out.println(pr.count(A));
}
}
I want to write a method that when supplied an array of ints will do the following. For each pair of array elements it will combine them and put them into a list of an inner class objects. Then it will compare each element in the array and check if it will fit between each pair values. (i.e. I have an array 0, 2, 4 it will make for example pair (0,4) and then it will check that value 2 is indeed between 0 and 4 so counter will increase). I tried the following code but it returned 0. How to fix it or is there an easier way to achieve that? I mainly care for the return value to be correct. thank you
import java.util.*;
import java.util.Map;
import java.lang.*;
public class Prac1 {
public int count(int[] A){
int k = 0;
class PTemp{
int first = -1;
int second = -1;
public PTemp(int first, int second){
int f = first;
int s = second;
}
}
List<PTemp> r = new ArrayList<PTemp>();
for (int i = 0; i < A.length; i++) {
for (int j = i+1; j < A.length; j++) {
r.add(new PTemp(A[i], A[j]));
r.add(new PTemp(A[j], A[i]));
//System.out.println("["+A[i] +","+A[j]+"]");
//System.out.println("["+A[j] +","+A[i]+"]");
}
}
Iterator<PTemp> ir = r.iterator();
while (ir.hasNext()){
PTemp p = ir.next();
for (int i = 0; i < A.length; i++){
if (((p.first < A[i]) && (A[i] < p.second)) || ((p.first > A[i]) && (A[i] > p.second))){
k = k + 1;
}
}
}
return k;
}
public static void main(String[] args){
int[] A = {0, 2, 4};
Prac1 pr = new Prac1();
System.out.println(pr.count(A));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 Alex D 提到的错误之外,还发现了第二个错误:
实际上应该是:
现在字段已正确设置。
Found a second bug in addition to the one mentioned by Alex D:
should really be:
Now the fields are properly set.
问题出在这里:
在我标记错误的行上,您将
int f
和int s
设置为first
和second< /code>,问题在于您的类
PTemp
的属性被称为first
和second
而不是f
> 和s
。改为此并重试:
The problem is here:
On the lines where I marked error, you set
int f
andint s
equal tofirst
andsecond
, the problem with this is that the attributes for your classPTemp
are calledfirst
andsecond
notf
ands
.Change to this and try again:
我刚刚看到了这个错误。这:
应该是:
编辑:看起来其他海报发现了另一个错误。我很高兴您的代码现在可以工作,但请注意,如果您在非常大的数组上使用它,它会非常非常慢。我已经发布了一种即使在大型数组上也能快速实现的方法,作为您其他相关问题的答案。
I just saw the bug. This:
should be:
EDIT: It looks like other posters found another bug. I'm glad that your code is working now, but please note that it will be very, very slow if you use it on a very large array. I have posted a way to make it fast, even on huge arrays, as an answer to your other, related question.