我的vector 中现有1,2,3,3,4;怎么找出3 并且打印出在vector 中有多少个?

解决方案 »

  1.   

    int a1[] ={1,3,3,3,4};
        Vector v = new Vector();
        int m = 0 ;
        for(int x:a1){
         v.add(a1[x]);
        }
        for(int i = 0 ; i <v.size();i++){
         if(v.get(i).equals(3)){
         m++;
         System.out.print(v.get(i)+" ");
         }
        }
        System.out.println("重复的个数   "+m);
        
    }}
      

  2.   

     int a1[] ={1,3,3,3,4}; 
        int time = 0;
        Vector<Integer> v = new Vector<Integer>();
        for(int i : a1 ){
           v.add(i);
        }
        for(int i = 0; i < v.size(); i++){
        if(-1 != v.lastIndexOf(3)){
         v.remove(v.lastIndexOf(3));
         time++;
        }
        }     if(time > 0){
         System.out.println("3共出现" + time + "次!");
        }else{
         System.out.println("未出现3!");
        }
      

  3.   

    或用apache提供的commons-collections 进行两个vector合并,然后对比两个size()
      

  4.   

    3楼的数组的遍历错了
    for(int x:a1){ 
        v.add(a1[x]); 
        } 不是a1[x],直接v.add(x)