将下列数组中奇数和偶数分别存放于两个不同的两个数组
/*
* 将下列数组中奇数和偶数分别存放于两个不同的两个数组
* */
public class Test09 {public static void main(String[] args) {int[] arr = {12,34,23,47,72,84,22,28,45,57,91};//用于统计偶数元素个数int o = 0;for (int i = 0;i< arr.length;i++){if (arr[i]%2==0){o++;}}//分别定义奇偶数组int[] os = new int[o];int[] js = new int[arr.length-o];//分别统计奇偶数组中元素的个数int num1 = 0;int num2 = 0;for (int x:arr){if (x%2==0){os[num1] = x;num1++;}else {js[num2] = x;num2++;}}//遍历奇偶数组for (int i:os){System.out.print(i+"\t");}System.out.println();for (int i:js){System.out.print(i+"\t");}}
}
执行代码结果如下: