数组的最小值/最大值

时间:2016-05-14 14:20:39

标签: java arrays sorting max min

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

  

“。java:126:error:类HighArray中的方法getMax无法应用于给定类型;”

     

“。java:126:error:类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();
    }
}

3 个答案:

答案 0 :(得分:3)

方法:

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

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

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

编辑:

你可能想修改你的方法不是 static 而不是有任何输入参数(数组a将直接从对象使用而不传递给方法) ,所以你可以在类的对象上使用这样的方法:arr.getMax();

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

  • public static int getMax(int[] a) - &gt; public long getMax()
  • public static int getMin(int[] a) - &gt; public long getMin()

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

答案 1 :(得分:0)

变量阴影是问题所在。

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

所以,

  1. 你真的不需要参数
  2. 您的数组具有长值,而不是int值
  3. 该方法不应该是静态的
  4. 代码

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

    同样的分钟

答案 2 :(得分: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;
}
相关问题