Min/Max Values of an Array

2019-09-25 15:49发布

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: 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;"

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

3条回答
太酷不给撩
2楼-- · 2019-09-25 16:26

Variable shadowing is the problem.

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

So,

  1. you really don't need the parameter
  2. your array has long values, not int values
  3. The method shouldn't be static

Code

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

Same for min

查看更多
迷人小祖宗
3楼-- · 2019-09-25 16:28

Methods:

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

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.

EDIT:

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

* 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.

查看更多
家丑人穷心不美
4楼-- · 2019-09-25 16:38

You have to change you getMin and getMax method.

It should also not be static otherwise you can't access the value array in that method. Also your array is of type long so the return value should be long too.

    // 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;
}
查看更多
登录 后发表回答