【问题标题】:Min/Max Values of an Array数组的最小值/最大值
【发布时间】:2016-09-10 16:48:11
【问题描述】:

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

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

【问题讨论】:

  • 仅供参考:下次尝试只发布相关的代码段。复制粘贴整个代码通常是不切实际的,并且不会让您快速得到答案。
  • 在你的课堂上实现maxmin的非静态版本,兼容long[]而不是int[],它会正常工作。

标签: java arrays sorting max min


【解决方案1】:

方法:

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

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

【讨论】:

  • 你还需要把int改成long
【解决方案2】:

你必须改变你的 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;
}

【讨论】:

    【解决方案3】:

    变量阴影是问题所在。

    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];  
    

    分钟相同

    【讨论】:

    • 我采纳了您的建议并修改了代码。现在我的问题是当我只需要一分钟时它会循环多次。 "77 99 44 55 22 88 -11 0 66 33 最大值99 最小值44 最小值22 最小值-11 找不到35 77 44 22 88 -11 66 33 "
    • 抱歉,读起来很糟糕哈哈。我没有意识到您不能按 Enter 键来添加空间。
    • 您的 print 语句在 for 循环中...您到底希望发生什么?
    • 是的,只是注意到了这一点,出于某种原因,我在编码时没有意识到或考虑到这一点。
    • @WilliamClarke A get 方法不应该打印任何内容,所以我假设这些是用于调试的,因此您可以在搜索最小值/最大值时看到进度。最后打印的是结果。如果您只想打印结果,则不应在 getXxx 方法中进行,而应在 main 方法中进行,例如System.out.print("The max value is " + arr.getMax());。我的意思是,该方法有一个返回值是有原因的,对吧?
    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2012-02-11
    • 2021-09-15
    • 2013-09-02
    • 2011-10-31
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多