日期:2014-05-20 浏览次数:20719 次
public static void main(String[] args) { List<Integer> A = new ArrayList<Integer>(); List<Integer> B = new ArrayList<Integer>(); for (int i = 0; i < 100; i++) { A.add(i); if(i == 99){ break; } B.add(i); } for (int i = 0; i < B.size(); i++) { A.remove(B.get(i)); } int result = A.get(0); System.out.println(result); }
------解决方案--------------------
public class Demo { private int[] A = new int[100]; private int[] B = new int[99]; private int random =0; private void initA(){ for(int i = 0; i < 100; i++) A[i] = i; } private void getBFromA(){ Random r=new Random(); random = r.nextInt(99); for(int i = 0; i < random; i++) B[i] = A[i]; for(int j = random; j < 99; j++) B[j] = A[j+1]; } public static void main(String[] args) { Demo d = new Demo(); d.initA(); d.getBFromA(); for(int i:d.A) System.out.print(i + " "); System.out.println(); for(int i:d.B) System.out.print(i + " "); System.out.println(); System.out.println("在A不在B:"+d.A[d.random]); } }