我想要对数组中的一些元素进行排序,但排除其他元素.
举一个简单的例子,一个包含整数的数组,我想对奇数进行排序,但将偶数保留在它们的位置.
到目前为止,我有以下内容:
public class MyClass {
public static void main(String args[]) {
int temp;
int array[] = {5, 3, 2, 8, 1, 4};
int[] sortedArray = new int[array.length];
for (int j = 0; j < array.length - 1; j++) {
for (int x = 0; x < array.length - 1; x++) {
if (array[x] > array[x + 1] && array[x] % 2 != 0 && array[x + 1] % 2 !=0) {
temp = array[x];
array[x] = array[x + …Run Code Online (Sandbox Code Playgroud)