Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_Invariants - Fatal编程技术网

Java 循环和不变量?

Java 循环和不变量?,java,loops,invariants,Java,Loops,Invariants,我试图交换数组b的值,并在k中存储一个值,以便后条件b[0…h]9为真 到目前为止,我有: for ( int b = 0; k!= b.length; k = k+1 ) { int p = k+1; for ( int h= k+1; h != b.length; h= h+1 ) { if ( b[h] < b[p] ){ p= h; } } int t = b[h]; b[h]= b[p

我试图交换数组b的值,并在k中存储一个值,以便后条件b[0…h]9为真

到目前为止,我有:

for ( int b = 0; k!= b.length; k = k+1 ) {
    int p = k+1;
    for ( int h= k+1; h != b.length; h= h+1 ) {
       if ( b[h] < b[p] ){ 
           p= h;
       }
    }

    int t = b[h]; 
    b[h]= b[p]; 
    b[p]= t;
}
for(int b=0;k!=b.length;k=k+1){
int p=k+1;
对于(int h=k+1;h!=b.length;h=h+1){
如果(b[h]
据我所知,您试图做的是获取一个
int
数组,对其排序,然后找到
n>9
所在的索引

实现这一点最简单的方法是使用
数组。sort
首先对数组进行排序,然后一个简单的for循环将显示最后一个索引的位置。然后可以将数组一分为二,使第一个数组包含所有值9

下面是一个例子:

public static void main(String[] args) {

    //Test data
    int[] arrayOfInt = {11, 42, 24, 1, 8, 9, 10, 15, 8, 29, 9, 34 };

    //Sort the array
    Arrays.sort(arrayOfInt);

    int index = 0;

    //This will get you the index where the last number is <= 9
    for (int i = 0; i < arrayOfInt.length; i++) {
        if (arrayOfInt[i] <= 9)
            index = i + 1;
    }

    //Now you know that from arrayOfInt[0] -> arrayOfInt[index] will be <= 9
    //If you want to use them seperately, you can just split the array into two
    int[] lessThanOrEqualToNine = Arrays.copyOfRange(arrayOfInt, 0, index);
    int[] greaterThanNine = Arrays.copyOfRange(arrayOfInt, index, arrayOfInt.length);

    System.out.println(Arrays.toString(lessThanOrEqualToNine));
    System.out.println(Arrays.toString(greaterThanNine));

    /* Prints the output:
     *  [1, 8, 8, 9, 9]
     *  [10, 11, 15, 24, 29, 34, 42]
     */
}
publicstaticvoidmain(字符串[]args){
//测试数据
int[]arrayOfInt={11,42,24,1,8,9,10,15,8,29,9,34};
//对数组进行排序
Arrays.sort(arrayOfInt);
int指数=0;

//这将为您提供索引,其中最后一个数字是我已经编辑了我的代码,但我不确定在哪里包含9。您的代码无法编译,您的问题很模糊。请参阅