JAVA中的数组,最小值的示例

JAVA中的数组,最小值的示例,java,Java,你能用一个例子/步骤解释一下这是如何工作的吗 public class MyClass { static void fors(int[] b) { for(int i = 0; i < b.length - 1; i++) { int o = b[i]; int p = b[i + 1]; if(o < p) b[i + 1] = o;

你能用一个例子/步骤解释一下这是如何工作的吗

public class MyClass {
    static void fors(int[] b) {
        for(int i = 0; i < b.length - 1; i++) {
            int o = b[i];
            int p = b[i + 1];
            if(o < p)
                b[i + 1] = o;
            if(i == b.length - 2)
                System.out.println(b[i + 1]);
        }
    }
    public static void main(String[] args) {
       int[] a = {2, 12, 42, 5, 9, 17, 20, 20, 3, 29, 80, 41, 1};
       fors(a);
    }
}
公共类MyClass{
静态空隙率(int[]b){
对于(int i=0;i

提前谢谢

算法将用较小的当前值替换下一个较大的值。根据您的代码,数组的结果如下: a={2,2,2,2,2,2,2,2,2,2,2,2,2,1}

如果你想找到最小的值

    //Assume the first one in the array the smallest first
    int smallest = b[0];

    //Then iterate the loop and compare the values
    for(int i = 0; i < b.length; i++) {
        //if the value is smaller than the current smallest, then which is the smallest?
        if(b[i] < smallest) 
            smallest = b[i];
    }
//假设数组中的第一个是最小的第一个
int最小值=b[0];
//然后迭代循环并比较值
for(int i=0;i
最佳解释——使用调试器,一步一步地检查代码,亲眼看看每一步都在做什么。您认为发生了什么?它给我们的值最小?大概