日期:2014-05-20  浏览次数:20961 次

很奇怪的一个方法?
需要达到的效果:
给方法callBack传入["3601"],["3602","3605","3601"] 需要输出 ["3602","3605"]
给方法callBack传入["3601","3605"],["3602","3605","3601"] 需要输出 ["3602"]
代码如下:
Java code

    public static String[] filterKey(String key,String[] jlKeyFrs){
         String resStr = ""; 
         if(key.length()==2){
             for(int j=0;j<jlKeyFrs.length;j++){
                 if(!jlKeyFrs[j].startsWith(key)){
                     resStr+=jlKeyFrs[j]+",";
                 }
             }
         }else{
             for(int j=0;j<jlKeyFrs.length;j++){
                 if(jlKeyFrs[j]!=key){
                     resStr+=jlKeyFrs[j]+",";
                 }
             }                         
         }
         
         if(resStr.length()>1){
             if(resStr.endsWith(",")){
                 resStr=resStr.substring(0, resStr.length()-1);
             }
         }
         return resStr.split(",");
    }
    
    public static String[] callBack(String[] wllKeys,String[] jlKeyFrs){
        String[] filterResults = null; 
        if(wllKeys.length == 1){
            filterResults = filterKey(wllKeys[0],jlKeyFrs);
            
        }else{
               String[] temps = filterKey(wllKeys[0],jlKeyFrs);
               String[] tempWllKeys = new String[wllKeys.length - 1];
               for(int k=0;k<tempWllKeys.length;k++){
                     tempWllKeys[k] = wllKeys[k+1];
               }
               callBack(tempWllKeys,temps);
        }
        return filterResults;
    }
    public static void main(String[] args) {
        String[] arr = callBack(new String[]{"3604","3603"},new String[]{"3604","3605","3603"});
        for(int i = 0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }




但运行过程中结果很奇怪,明明相等JAVA却判断不相等
麻烦各位大神看看问题出在哪了?

------解决方案--------------------
判断相等,使用equals,不要使用==
------解决方案--------------------
看不懂的路过...
------解决方案--------------------
Java code
 public static String[] callBack(String[] wllKeys,String[] jlKeyFrs){
        String[] filterResults = null; 
        if(wllKeys.length == 1){
            filterResults = filterKey(wllKeys[0],jlKeyFrs);
            
        }else{
              // 你所传递的是length为2,但在else里没有返回值。
                // filterKey方法中第一个参数既然是一个字符串,if(key.length()==2)你要判断它的长度为2位? 如 05,而不是3604 4位?
               String[] temps = filterKey(wllKeys[0],jlKeyFrs);
               String[] tempWllKeys = new String[wllKeys.length - 1];
               for(int k=0;k<tempWllKeys.length;k++){
                     tempWllKeys[k] = wllKeys[k+1];
               }
               callBack(tempWllKeys,temps);
        }
        return filterResults;