数组的最小值/最大值 [英] Min/Max Values of an Array

查看:105
本文介绍了数组的最小值/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎停留在似乎很基本的任务上.我想知道是否有人可以引导我朝正确的方向并向我解释什么地方错了.我创建了一个插入了预制值的数组.现在,我必须获取此数组的最小值/最大值并显示它们.我不断收到这两个错误

I seem to be stuck on what seems to be a pretty basic assignment. I was wondering if anyone could possibly lead me to the right direction and explain to me what is wrong. I have created an array with premade values inserted. Now I have to get the min/max values of this array and display them as well. I keep getting these two errors

.java:126:错误:类HighArray中的方法getMax无法应用于给定类型;"

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

.java:126:错误:类HighArray中的方法getMin无法应用于给定类型;"

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

如果有人可以帮助我并解释原因,将不胜感激.谢谢!

If anyone could possibly help me out and explain why this is, it would be greatly appreciated. Thank you!

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)
  • public static int getMax(int[] a) and
  • public static int getMin(int[] a)

int[]作为其输入参数,
但随后会调用它们而没有任何参数:arr.getMax();arr.getMin();.

have int[] as their input parameter,
but they are later called without any parameters: arr.getMax(); and arr.getMin();.

这是您从编译器得到错误的原因.

This is the cause of the error you are getting from the compiler.

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

You probably want to modify your methods not to be static and not to have any input parameters (the array a would be used directly from the object and not passed to the method), so you can use methods on the object of the class like this: arr.getMax();.

为此,可以通过以下方式更改代码:

To do so change the code in the following way:

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

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

* Note: The return type of getMax and getMin methods is changed from int to long, because longis the type of array in the HighArray class.

这篇关于数组的最小值/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆