從我瞭解你正在試圖做的,是採取一個數組的int
,排序,然後找到索引,其中,n > 9
。
實現此目的的最簡單方法是使用Arrays.sort
首先對數組進行排序,然後簡單的for循環將顯示最後一個索引的位置。然後,您可以在兩個陣列分裂,所以第一個數組包含所有值< = 9,而第二個包含所有> 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]
*/
}
我已經編輯我的代碼,但我不確定在哪裏包括9. – 2012-04-18 05:31:25
您的代碼無法編譯,您的問題含糊不清。請參閱[SSCCE](http://sscce.org/) – tanyehzheng 2012-04-18 06:25:53