第一个长度为10的整形数组,里面从10-20要求把这个数组里的奇数放在一个数组中,偶数放在一个数组中
/**
* @author BoBooY
* @date 2022/9/25 17:03
*/
public class Test {
public static void main(String[] args) {
// 初始数组
int[] arrInit = new int[]{11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int flagOdd = 10; // 定义奇数数组的长度
int[] arrOdd = new int[flagOdd]; // 定义一个存放奇数的数组
int flagEven = 10; // 定义偶数数组的长度
int[] arrEven = new int[flagEven]; // 定义一个存放偶数的数组
for (int i = 0; i < 10; i++) {
if (arrInit[i] % 2 == 0) { // 判断是否是偶数
arrOdd[flagOdd - 10] = arrInit[i];
flagOdd++;
} else {
arrEven[flagEven - 10] = arrInit[i];
flagEven++;
}
}
System.out.println("奇数数组" + Arrays.toString(arrOdd));
System.out.println("偶数数组" + Arrays.toString(arrEven));
}
}
10-20是21个数,到底哪些要放到10个元素的数组呢?
你定义两个数组,然后对10-20元素的数组进行循环遍历,如果元素值求余2为0,就是偶数,加入偶数数组,反之加入奇数数组