Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 - Fatal编程技术网

Java 数组是否已排序?

Java 数组是否已排序?,java,arrays,sorting,Java,Arrays,Sorting,我的作业有问题。我的任务仅打印否。我的条件是编写一个程序,确定整数数组是否按递增顺序排序 示例: 投入:12348123420000 输出:是 输入:7 8 9 10 11 11 13 9 奥普特:没有 在比较元素时,需要使用循环 使用arr[0]=sc.nextInt()只在索引0处添加了元素。它应该是arr[i]=sc.nextInt() 另一个示例运行: import java.util.Arrays; import java.util.Scanner; public class Mai

我的作业有问题。我的任务仅打印否。我的条件是编写一个程序,确定整数数组是否按递增顺序排序

示例:

投入:12348123420000 输出:是

输入:7 8 9 10 11 11 13 9 奥普特:没有

  • 在比较元素时,需要使用循环
  • 使用
    arr[0]=sc.nextInt()只在索引
    0
    处添加了元素。它应该是
    arr[i]=sc.nextInt()
  • 另一个示例运行:

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter the size of the array: ");
            int length = sc.nextInt();
            int[] arr = new int[length];
            System.out.print("Enter " + length + " integers: ");
            for (int i = 0; i < length; i++) {
                arr[i] = sc.nextInt();
            }
            boolean isSorted = true;
            for (int i = 2; i < arr.length; i++) {
                if (arr[i - 2] > arr[i - 1]) {
                    isSorted = false;
                    break;
                }
            }
            if (isSorted) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
    
    
    Enter the size of the array: 5
    Enter 5 integers: 5 1 4 3 2
    No
    

    您应该使用for循环迭代来检查数组中的所有元素。
    if-else
    语句对于检查整个数组没有用处

    Enter the size of the array: 5
    Enter 5 integers: 1 2 3 4 5
    Yes
    
    for(int i=0;i=下一个){//如果上一个>大于下一个
    isSorted=false;/…数组不再排序
    中断;/…退出迭代(不允许继续)
    }
    }
    
    我有一个函数形式的单行程序:

    for (int i = 0; i < arr.length - 1; i++) { // iterate from 0 to n-1 to avoid ArrayIndexOutOfBoundsException
        int current = arr[i];                  // the x-th element
        int next = arr[i+1];                   // the (x+1)th element
        if (current >= next) {                 // if the previous > than the following one
            isSorted = false;                  // ... the array is no longer sorted
            break;                             // ... exit the iteration (makes no secne to continue)
        }
    }
    
    publicstaticbooleanissorted(int[]数组){
    
    返回IntStream.range(0,array.length-1).allMatch(i->array[i]输入输出1 2 4 8 12 34 200 2000是输入输出7 8 9 10 11 12 11 13 9不谢谢你。但我能在我的程序中做些什么才是正确的。@KristinaMihailova-我已经更新了答案。如果有任何疑问/问题,请随时发表评论。@KristinaMihailova-别忘了回答回答问题,以便未来的访问者也能自信地使用解决方案。检查以了解如何操作。如果有任何疑问/问题,请随时发表评论。
    for (int i = 0; i < arr.length - 1; i++) { // iterate from 0 to n-1 to avoid ArrayIndexOutOfBoundsException
        int current = arr[i];                  // the x-th element
        int next = arr[i+1];                   // the (x+1)th element
        if (current >= next) {                 // if the previous > than the following one
            isSorted = false;                  // ... the array is no longer sorted
            break;                             // ... exit the iteration (makes no secne to continue)
        }
    }
    
    public static boolean isSorted(int[] array) {
        return IntStream.range(0, array.length - 1).allMatch(i -> array[i] <= array[i + 1]);
    }