如何计算给定数组的两个元素之间的数组元素的数量

发布于 2024-12-31 21:35:20 字数 1607 浏览 1 评论 0原文

我想编写一个方法,当提供一个整数数组时,该方法将执行以下操作。对于每对数组元素,它将组合它们并将它们放入内部类对象的列表中。然后它将比较数组中的每个元素并检查它是否适合每对值。 (即我有一个数组 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 技术交流群。

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

发布评论

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

评论(3

枕花眠 2025-01-07 21:35:20

除了 Alex D 提到的错误之外,还发现了第二个错误:

class PTemp{        
        int first = -1;
        int second = -1;
        public PTemp(int first, int second){
            int f = first;
            int s = second;             
        }           
    }

实际上应该是:

class PTemp{        
        int first = -1;
        int second = -1;
        public PTemp(int first, int second){
            this.first = first;
            this.second = second;             
        }           
    }

现在字段已正确设置。

Found a second bug in addition to the one mentioned by Alex D:

class PTemp{        
        int first = -1;
        int second = -1;
        public PTemp(int first, int second){
            int f = first;
            int s = second;             
        }           
    }

should really be:

class PTemp{        
        int first = -1;
        int second = -1;
        public PTemp(int first, int second){
            this.first = first;
            this.second = second;             
        }           
    }

Now the fields are properly set.

蒗幽 2025-01-07 21:35:20

问题出在这里:

 class PTemp
 {        
    int first = -1;
    int second = -1;
    public PTemp(int first, int second)
    {
       int f = first; //error
       int s = second; //error      
    }           
 }

在我标记错误的行上,您将 int fint s 设置为 firstsecond< /code>,问题在于您的类 PTemp 的属性被称为 firstsecond 而不是 f > 和 s

改为此并重试:

     class PTemp
     {        
        int first = -1;
        int second = -1;

        public PTemp(int first, int second)
        {
           this.first = first;
           this.second = second;             
        }           
     }

The problem is here:

 class PTemp
 {        
    int first = -1;
    int second = -1;
    public PTemp(int first, int second)
    {
       int f = first; //error
       int s = second; //error      
    }           
 }

On the lines where I marked error, you set int f and int s equal to first and second, the problem with this is that the attributes for your class PTemp are called first and second not f and s.

Change to this and try again:

     class PTemp
     {        
        int first = -1;
        int second = -1;

        public PTemp(int first, int second)
        {
           this.first = first;
           this.second = second;             
        }           
     }
人间☆小暴躁 2025-01-07 21:35:20

我刚刚看到了这个错误。这:

r.add(new PTemp(i, j));
r.add(new PTemp(j, i));

应该是:

r.add(new PTemp(A[i], A[j]));
r.add(new PTemp(A[j], A[i]));

编辑:看起来其他海报发现了另一个错误。我很高兴您的代码现在可以工作,但请注意,如果您在非常大的数组上使用它,它会非常非常慢。我已经发布了一种即使在大型数组上也能快速实现的方法,作为您其他相关问题的答案。

I just saw the bug. This:

r.add(new PTemp(i, j));
r.add(new PTemp(j, i));

should be:

r.add(new PTemp(A[i], A[j]));
r.add(new PTemp(A[j], A[i]));

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.

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