java好难
给顶数组{12,45,32,78,54,9,37,89,43,67,10,50}把数值是偶数的放一个数组,是奇数的放一个数组
import java.util.ArrayList; public class OddEvenNum { private int[] num = {12,45,32,78,54,9,37,89,43,67,10,50}; private int[] oddNum; //奇数 private int[] evenNum; public void separate(){ ArrayList<Integer> odd = new ArrayList<Integer>(); ArrayList<Integer> even = new ArrayList<Integer>(); for(int i=0; i<num.length; i++){ if((num[i]&1)==0){ even.add(num[i]); } else odd.add(num[i]); } //转换成数组 oddNum = new int[odd.size()]; evenNum = new int[even.size()]; for(int i=0; i<odd.size(); i++) oddNum[i]=odd.get(i); for(int i=0; i<even.size(); i++) evenNum[i]=even.get(i); } public void print(){ System.out.println("奇数序列:"); for(Object i:oddNum) System.out.print(i + " "); System.out.println("\n偶数序列:"); for(Object i:evenNum) System.out.print(i + " "); } public static void main(String[] args) { OddEvenNum test = new OddEvenNum(); test.separate(); test.print(); } }