Java 用于检查以输出数组中最大值或显示(如果为空)的算法

Java 用于检查以输出数组中最大值或显示(如果为空)的算法,java,algorithm,Java,Algorithm,在开始我的计算机科学本科学习之前,我试图练习算法,而我在写算法方面做得非常糟糕。一旦我被教导并打破它们,我就会理解它们,但当我试图做我自己的事情时,它失败了。我在一本编程教科书中尝试一个练习题,其中我有一个数组,我必须输出最大值,或者如果数组为空,我必须显示-1 这是我能想出的最好的办法,但仍然远远不够。任何关于我到底做错了什么的指示 for(i = 0;i < array.length-1;i++) if(array[i] == 0){ empty = tr

在开始我的计算机科学本科学习之前,我试图练习算法,而我在写算法方面做得非常糟糕。一旦我被教导并打破它们,我就会理解它们,但当我试图做我自己的事情时,它失败了。我在一本编程教科书中尝试一个练习题,其中我有一个数组,我必须输出最大值,或者如果数组为空,我必须显示-1

这是我能想出的最好的办法,但仍然远远不够。任何关于我到底做错了什么的指示

 for(i = 0;i < array.length-1;i++)
     if(array[i] == 0){
         empty = true;
         n = -1;
         System.out.println(n);
     }else{
         largest = array[0];
         if(array[i] > largest){
             largest = array[i];
             System.out.println(array[i]);
         }
     }
for(i=0;i最大值){
最大=数组[i];
System.out.println(数组[i]);
}
}
我看到-1显示了10次,但我没有找到解决方案,如果数组已满,它会为我提供一个接一个的值。

if(array.length==0){
 if(array.length == 0){
     empty = true;
     n = -1;
     System.out.println(n);
 }else{
     largest = array[0];
     for(i = 1; i < array.length; i++)
         if(array[i] > largest){
             largest = array[i];
     }
     System.out.println(largest);
 }
空=真; n=-1; 系统输出println(n); }否则{ 最大=数组[0]; 对于(i=1;i最大值){ 最大=数组[i]; } 系统输出打印项次(最大); }

首先我要做的是检查数组是否为空。如果是,则跳过else并输出-1。如果数组不是空的,则将最大值设置为第一个元素,然后循环所有其他元素。注意到我更改了I=1,因为元素0已经是最大的。我还删除了数组长度的-1,因为我将在长度之前停止1(这将是最终元素的最后一个索引)。最后,我将最大值的打印移到if语句和for循环之外,以便在最后只打印一次

您正在检查每个循环中数组的长度。这意味着它不会被检查,除非数组有一些对象,这正是我们不想要的。 但实际上,您并没有检查数组长度:

if(array[i]==0)
测试索引
i
处的数组项是否为0。要测试数组的长度,必须执行
if(array.length>0)
。请记住,在for循环之前必须执行此操作,因此它是这样的:

if(array[i] == 0){
     empty = true;
     n = -1;
     System.out.println(n);
}
for(i = 0;i < array.length-1;i++)
     largest = array[0];
     if(array[i] > largest){
         largest = array[i];
         System.out.println(array[i]);
     }
 }
有几件事:

if(array[i] == 0)
不检查数组是否为空,它检查存储在数组索引i处的值是否等于零。您可能需要以下内容:

if(array != null && array.length == 0) { 
    return -1;
}
在for循环执行之前

if(array[i] == 0){
     empty = true;
     n = -1;
     System.out.println(n);
}
largest = array[0];
for(i = 0;i < array.length-1;i++)
     if(array[i] > largest){
         largest = array[i];
         System.out.println(array[i]);
     }
 }

代码的其余部分看起来很接近。else子句中的print语句是不必要的。您只需要在for循环执行后返回max的值。

这段代码通常比较奇怪。为什么当你到达一个0的元素时,你会认为数组是空的?如果数组为空,为什么要继续搜索?最后但并非最不重要的一点:为什么要忽略数组中的最后一个元素

针对这一点,有很多简单得多的解决方案:

没有任何java api:

if(array.length == 0)
    return -1;

int max = Integer.MIN_VALUE;
for(int i = 0 ; i < array.length ; i++)
    if(max < array[i])
        max = array[i];

return max;
使用java8:

return Arrays.stream(array).min((a , b) -> new Integer(a).compareTo(b)).orElse(-1);

让我们看看你的代码

for(i = 0;i < array.length-1;i++)//You will never reach the last number
 if(array[i] == 0){//Here you check if the first item equals zero. Gives error if there is no first item.
     empty = true;//why do you need this, you never use it.
     n = -1;
     System.out.println(n);
 }else{
     largest = array[0];//Why setting largest, do dont know if it is actually bigger
     if(array[i] > largest){
         largest = array[i];
         System.out.println(array[i]);
     }
 }
for(i=0;i最大值){
最大=数组[i];
System.out.println(数组[i]);
}
}
改进版:

largest = -1;//set it by default
for(i = 0;i < array.length;i++)//if length is 10 iMax = 9, which is the tenth item.
    if(array[i]>largest){//if larger set the new value
        largest = array[i];
    }   
 }
 System.out.println(largest);//print the largest number.
max=-1//默认设置它
对于(i=0;i最大值){//if较大值设置新值
最大=数组[i];
}   
}
系统输出打印项次(最大)//打印最大的数字。

首先检查数组是否为空

if(array != null && array.length == 0) {
如果它是print-1

    System.out.println(-1 + "");
}
现在您已准备好遍历阵列。但在创建一个变量来跟踪最大值之前。我将它设置为数组中的第一个元素(因为我们知道它至少有一个元素,因为我们已经确定它不是空的)

然后,循环遍历数组

    for(i = 0;i < array.length-1;i++) {
    }
然后将其与最大的进行比较。如果它大于目前为止的最大值,请将其另存为最大值

        if (current>largest) {
            current = largest;
        }
继续这样做,直到完成阵列循环

    for(i = 0;i < array.length-1;i++) {
    }
然后打印出最大的

    System.out.println(largest + "");
}

如果你在与算法作斗争,有时不把它们视为算法而是现实世界的问题会有所帮助

假设你在一家商店里买奶酪,你的任务是挑选一包过期日期最晚的奶酪。现在想象一下情况:站在奶酪区寻找最好的美味奶酪

  • 首先,你看看有没有。如果没有,空手而归
  • 如果有一些包,仔细检查,寻找最好的。你可能只有一只手空着,另一只手拿着篮子,所以

    • 首先,挑选第一块奶酪
    • 然后逐一检查包装。如果你发现奶酪比你手上的好,就把你拿着的那一块放下来,拿一块更好的
  • 现在,让我们把它放到一种编程语言中:

    int[] cheeseExpirations = new int[] { ... };
    if (cheeseExpirations.length == 0) {
        System.out.println(-1); // no cheese :(
    } else {
        int myCheese = cheeseExpirations[0];                 // take the first pack; better a sparrow in the hand than a pigeon on the roof...
        for (int i = 0; i < cheeseExpirations.length; i++) { // idiomatic array iteration
            int currentCheese = cheeseExpirations[i];
            if (currentCheese > myCheese ) {                 // found a better one
                myCheese = currentCheese;                    // just take it
            }
        }
        System.out.println(bestCheese);
    }
    
    int[]cheeseExpirations=newint[]{…};
    if(cheeseExpirations.length==0){
    System.out.println(-1);//没有奶酪:(
    }否则{
    int myCheese=cheeseExpirations[0];//拿第一包;手上有麻雀总比屋顶上有鸽子好。。。
    对于(inti=0;imyCheese){//找到了更好的
    myCheese=currentCheese;//只要我
    
    int[] cheeseExpirations = new int[] { ... };
    if (cheeseExpirations.length == 0) {
        System.out.println(-1); // no cheese :(
    } else {
        int myCheese = cheeseExpirations[0];                 // take the first pack; better a sparrow in the hand than a pigeon on the roof...
        for (int i = 0; i < cheeseExpirations.length; i++) { // idiomatic array iteration
            int currentCheese = cheeseExpirations[i];
            if (currentCheese > myCheese ) {                 // found a better one
                myCheese = currentCheese;                    // just take it
            }
        }
        System.out.println(bestCheese);
    }