Min/Max 数组的值

Min/Max Values of an Array

我似乎被困在一个看似非常基本的任务上。我想知道是否有人可以引导我走向正确的方向并向我解释哪里出了问题。我创建了一个插入了预制值的数组。现在我必须获取此数组的 min/max 值并显示它们。我一直收到这两个错误

".java:126: error: method getMax in class HighArray cannot be applied to given types;"

".java:126: error: method getMin in class HighArray cannot be applied to given types;"

如果有人可以帮助我并解释这是为什么,将不胜感激。谢谢!

class HighArray
{
    private long[] a;
    private int nElems;

    public HighArray(int max)
    {
        a = new long[max];
        nElems = 0;
    }

   //Search Method 
    public boolean find(long searchKey)
    {
        int j;
        for(j=0; j<nElems; j++)
            if(a[j] == searchKey)
                break;
        if(j == nElems)
            return false;
        else
            return true;
    }

    //Insert method     
    public void insert(long value)
    {
        a[nElems] = value;
        nElems++;
    }

    //Delete method        
    public boolean delete(long value)
    {
        int j;
        for(j=0; j<nElems; j++)
            if( value == a[j] )
                break;
        if(j==nElems)
            return false;
        else
        {
            for(int k=j; k<nElems; k++)
                a[k] = a[k+1];
            nElems--;
            return true;
        }
    }

    //Display Array Contents 
    public void display()
    {
        for(int j=0; j<nElems; j++)
            System.out.print(a[j] + " ");
        System.out.println(" ");
    }

    //Max Method 
    public static int getMax(int[] a)
    {
        int maxValue = a[0];
        for(int i=1;i < a.length;i++)
        {
            if(a[i] > maxValue)
            {
                maxValue = a[i];
                System.out.print("The max value is" + a[i]);
            }
        }
        return maxValue;
    }

    //Min Method
    public static int getMin(int[] a)
    {
        int minValue = a[0];
        for(int i=1;i<a.length;i++)
        {
            if(a[i] < minValue)
            {
                minValue = a[i];
                System.out.print("The min value is" + a[i]);
            }
        }
        return minValue;
    }
}

public class Assignment
{
    public static void main(String[] args)
    {
        int maxSize = 100;
        HighArray arr = new HighArray(maxSize);
        arr.insert(77);
        arr.insert(99);
        arr.insert(44);
        arr.insert(55);
        arr.insert(-22);
        arr.insert(88);
        arr.insert(-11);
        arr.insert(00);
        arr.insert(66);
        arr.insert(-33);

        arr.display();
        arr.getMax();
        arr.getMin();

        int searchKey = 35;
        if( arr.find(searchKey) )
            System.out.println("Found" + searchKey);
        else
            System.out.println("Can't Find " + searchKey);

        arr.delete(00);
        arr.delete(55);
        arr.delete(99);

        arr.display();
    }
}
           

方法:

  • public static int getMax(int[] a)
  • public static int getMin(int[] a)

int[]作为他们的输入参数,
但它们后来在没有任何参数的情况下被调用:arr.getMax();arr.getMin();

这就是您从编译器收到错误的原因。

编辑:

您可能希望将您的方法修改为不 static 并且不具有任何输入参数(数组 a 将直接从对象使用而不是传递给方法),因此您可以像这样在 class 的对象上使用方法:arr.getMax();.

为此,按以下方式更改代码:

  • public static int getMax(int[] a) --> public long getMax()
  • public static int getMin(int[] a) --> public long getMin()

* 注:getMaxgetMin方法的return类型由int改为long,因为longHighArray中数组的类型class.

变量阴影是问题所在。

public static int getMax(int[] a) // <-- this a is different than the other one 
  {  
  int maxValue = a[0];  

所以,

  1. 你真的不需要这个参数
  2. 你的数组有 long 值,不是 int 值
  3. 方法不应该是静态的

代码

public long getMax() 
  {  
  long maxValue = a[0];  

最小值相同

您必须更改 getMin 和 getMax 方法。

它也不应该是静态的,否则您无法在该方法中访问值数组。 此外,您的数组是 long 类型,因此 return 值也应该很长。

    // Max Method

public long getMax() {
    long maxValue = a[0];

    for (int i = 1; i < a.length; i++) {

        if (a[i] > maxValue) {
            maxValue = a[i];
            System.out.println("The max value is" + a[i]);

        }
    }
    return maxValue;
}

// Min Method

public  long getMin() {
    long minValue = a[0];

    for (int i = 1; i < a.length; i++) {

        if (a[i] < minValue) {
            minValue = a[i];
            System.out.println("The min value is" + a[i]);
        }
    }

    return minValue;
}