java 代码
- package com.ideal.grid;
- import java.util.Vector;
- public class Miao {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //比较2个数组
- //备注:所谓的增加或减少 是拿后边的数组和前边的数组比较
- String [] A1 = {"A","B","C","D"};
- String [] A2 = {"E","D","F","B"};
- Vector vc = new Vector();
- Vector vc2 = new Vector();
- for (int i = 0; i < A1.length; i++) {
- String str1 = A1[i];
- for (int j = 0; j < A2.length; j++) {
- String str2 = A2[j];
- if(str1.equals(str2)){
- vc.remove(str1);
- break;
- }
- else{
- if(!vc.contains(str1)){
- vc.add(str1);
- }
- }
- }
- }
- for (int i = 0; i < A2.length; i++) {
- String str1 = A2[i];
- for (int j = 0; j < A1.length; j++) {
- String str2 = A1[j];
- if(str1.equals(str2)){
- vc2.remove(str1);
- break;
- }
- else{
- if(!vc2.contains(str1)){
- vc2.add(str1);
- }
- }
- }
- }
- System.out.println("开始打印比较结果");
- System.out.println("开始打印增加的元素");
- for(int i = 0 ; i < vc.size();i++){
- System.out.println(vc.get(i));
- }
- System.out.println("开始打印减少的元素");
- for(int i = 0 ; i < vc2.size();i++){
- System.out.println(vc2.get(i));
- }
- }
- }
对于整形数组的比较
java 代码
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[] ={1,3,5,9,2};
- int b[] ={2,3,9,8,6};
- Vector vc = new Vector();
- for (int i = 0; i < a.length; i++) {
- int aa = a[i];
- for (int j = 0; j < b.length; j++) {
- int bb = b[j];
- if(aa==bb){
- vc.remove(new Integer(aa));
- break;
- }else{
- if(!vc.contains(new Integer(aa))){
- vc.add(new Integer(aa));
- }
- }
- }
- }
- System.out.println("打印a有b没有的元素");
- for (int i = 0; i < vc.size(); i++) {
- System.out.println(vc.get(i));
- System.out.println("====================");
- }
- }
- }