Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组的最小/最大值_Java_Arrays_Sorting_Max_Min - Fatal编程技术网

Java 数组的最小/最大值

Java 数组的最小/最大值,java,arrays,sorting,max,min,Java,Arrays,Sorting,Max,Min,我似乎被一项基本的任务困住了。我想知道是否有人能把我引向正确的方向,并向我解释什么是错的。我已经创建了一个插入了预制值的数组。现在我必须得到这个数组的最小/最大值并显示它们。我一直在犯这两个错误 “.java:126:错误:无法将类HighArray中的方法getMax应用于给定类型;” “.java:126:错误:无法将HighArray类中的方法getMin应用于给定类型;” 如果有人能帮我解释一下原因,我将不胜感激。谢谢大家! class HighArray { private l

我似乎被一项基本的任务困住了。我想知道是否有人能把我引向正确的方向,并向我解释什么是错的。我已经创建了一个插入了预制值的数组。现在我必须得到这个数组的最小/最大值并显示它们。我一直在犯这两个错误

“.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();
    }
}
           
class高级数组
{
私人长[]a;
内勒姆私人酒店;
公共高位数组(整数最大值)
{
a=新长[最大值];
nElems=0;
}
//搜索方法
公共布尔查找(长搜索键)
{
int j;
对于(j=0;j方法:

  • 公共静态int getMax(int[]a)
  • publicstaticintgetmin(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()

*注意:
getMax
getMin
方法的返回类型从
int
更改为
long
,因为
long
HighArray
类中的数组类型。

变量阴影是问题所在

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

最小值也是一样,您必须更改getMin和getMax方法

它也不应该是静态的,否则无法访问该方法中的值数组。 此外,数组的类型为long,因此返回值也应为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;
}
//Max方法
公共长getMax(){
长最大值=a[0];
for(int i=1;imaxValue){
最大值=a[i];
System.out.println(“最大值为”+a[i]);
}
}
返回最大值;
}
//最小法
公共长getMin(){
长最小值=a[0];
for(int i=1;i
仅供将来参考:下次试着只发布相关的代码段。复制粘贴整个代码通常是不切实际的,也不会给你一个快速的答案。实现非静态版本的
max
&
min
,与
long[]
兼容,而不是
int[]
,在你的课堂上,它会正常工作。我采纳了你的建议并修改了代码。现在我的问题是它在我只需要一分钟的情况下多次循环min。“77 99 44 55 22 88-11 0 66 33最大值为99最小值为44最小值为22最小值为-11找不到35 77 44 22 88-11 66 33"很抱歉,读起来很糟糕。哈哈。我不知道你不能按enter键来添加空间。你的打印语句在for循环中…你到底期望发生什么?是的,只是注意到,出于某种原因,我在编码时没有意识到或考虑到这一点。@WilliamClarke A
get
方法不应该打印任何内容,所以我假设ose用于调试,因此您可以在搜索最小/最大值时看到进度。最后一次打印是结果。如果您只想打印结果,则不应该在
getXxx
方法中执行,而应该在
main
方法中执行,例如
System.out.print(“最大值为”+arr.getMax())
。我的意思是,这个方法有一个返回值是有原因的,对吗?您还需要将
int
更改为
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;
}