添加数组,list简便转换数组等

本人对数组运用的特别多,所以不是特别熟悉,现在基本用的集合,但现在需要使用数组,所以整理自己业务上对于数组的操作。

String regionCodes = getPara("regionCodes");//从web页面得到参数值

//indexOf()  判断是否含有某个字符串  <0:不包含

if(regionCodes.indexOf(",") >= 0){
            String isF[] = null;
            String notF[] = null;
            String codes[] = regionCodes.split(",");//根据符号“,”分割成数组
            for(int i = 0;i<codes.length;i++){//循环数组
                if(codes[i].indexOf("F") < 0){
                    List<Resource> resource = Resource.dao.find("xxx",codes[i]);//数据库中得到数据
                    List<String> resource_code = new ArrayList<>();
                    for(int j = 0;j < resource.size();j++){
                        resource_code.add(resource.get(j).getCode());//生成List<String>
                    }
                    isF = new String[resource_code.size()];//初始化数组长度

//因为 isF 是String 类型,则resource_code 中的范类也只能是String 类型,否则会抛出java.lang.ArrayStoreException
                    isF = resource_code.toArray(isF);//将list转换为数组(需要注意,转换数组是什么类型)
                }else{
                    notF = new String[codes.length];

                    //数组赋值
                    notF[i] = codes[i];
                }
            }

           //数组code 与 isF数组 合并
            code = (String[]) ArrayUtils.addAll(code, isF);

           //已经与isF数组合并后的数组 与 notF数组合并
            code = (String[]) ArrayUtils.addAll(code, notF);
        }else{

           //直接初始化数组    String code[] = new String[]{"111111"}
            code = new String[]{regionCodes};
        }

       
        List<String> tmp = new ArrayList<String>();

        //遍历数组
        for(String str : code){
            if(str!=null && str.length()!=0 && !str.equals("null")){
                tmp.add(str);
            }else{
                continue;
            }
        }

         //返回数组
        return tmp.toArray(new String[0]);

猜你喜欢

转载自blog.csdn.net/fearlessnesszhang/article/details/81117926
今日推荐