繁体   English   中英

数组的最小值/最大值

[英]Min/Max Values of an Array

我似乎被困在一个非常基本的任务上。 我想知道是否有人可以引导我走向正确的方向并向我解释什么是错的。 我创建了一个插入了预制值的数组。 现在我必须得到这个数组的最小值/最大值并显示它们。 我不断收到这两个错误

“.java:126: 错误:HighArray 类中的方法 getMax 不能应用于给定类型;”

“.java:126: 错误:HighArray 类中的方法 getMin 不能应用于给定类型;”

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

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

这是您从编译器获得错误的原因。

编辑:

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

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

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

* 注意: getMaxgetMin方法的返回类型由int改为long ,因为longHighArray类中的数组类型。

变量阴影是问题所在。

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 类型,因此返回值也应该很长。

    // 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM