华为机试题: 将数组中的字符串按指定长度重新分割(java)

来源:互联网 发布:java 参数 编辑:程序博客网 时间:2024/06/01 10:15

描述: 

请实现接口 convertStringArray。

输入一个字符串数组, 请按指定长度iInputLenth拆分数组中的每个字符串,输出到新的字符串数组中。长度不是iInputLenth整数倍的字符串请在后面补数字0。空字符串不处理,遇到空字符串则表示该数组结束。

/*

    功能:请编写一个函数,输入为一个字符串数组,

    请按指定长度iInputLenth拆分数组中的每个字符串,输出到新的字符串数组中。长度不是iInputLenth整数倍的字符串请在后面补数字0

    空字符串不处理,遇到空字符串则表示数组结束,函数返回。

    输入:

        String inputStrArray     字符串数组指针 字符串个数最大为50,字符串长度最大255

        int iInputLenth   指定的分割长度 iInputLenth>=1 && <=32 <><=32 <><=32 <>

         

    返回:字符串数组指针     

 

    示例 按长度8拆分

    输入: abc 

           12345789 

    返回: abc00000

           12345678

           90000000

 

    */

 

    public static String[] convertStringArray(String[] inputStrArray, int iInputLenth)

    {

        return null;

    }


package huawei;import java.util.Vector;public final class Demo {/*功能:请编写一个函数,输入为一个字符串数组,请按指定长度iInputLenth拆分数组中的每个字符串,输出到新的字符串数组中。长度不是iInputLenth整数倍的字符串请在后面补数字0。空字符串不处理,遇到空字符串则表示数组结束,函数返回。输入:    String inputStrArray     字符串数组指针 字符串个数最大为50,字符串长度最大255    int iInputLenth   指定的分割长度 iInputLenth>=1 && <=32      返回:字符串数组指针     示例 按长度8拆分输入: abc        12345789 返回: abc00000       12345678       90000000*/public static String[] convertStringArray(String[] inputStrArray, int iInputLenth){/*入参判断*/if (iInputLenth < 1 || iInputLenth > 32){return null;}Vector<String> vec = new Vector<String>();//保存生成的字符串for(int i = 0; i < inputStrArray.length; i++){/*把这个字符串取出来*/String str = inputStrArray[i];char[] src = str.toCharArray();/*这个字符串不为空*/if(str.length() > 0){int curIndex = 0;for(; (curIndex + iInputLenth) < str.length(); curIndex += iInputLenth){/*新建一个StringBuilder对象*/StringBuilder temp = new StringBuilder();for(int j = 0; j < iInputLenth; j ++){temp.append(src[curIndex + j]);}vec.add(temp.toString());}int count = 0;StringBuilder temp1 = new StringBuilder();for(; curIndex < str.length(); curIndex++){temp1.append(src[curIndex]);count++;}/*补0*/for(; count < iInputLenth; count++){temp1.append('0');}vec.add(temp1.toString());}}if(vec.size() > 0){String[] ret = new String[vec.size()];ret= vec.toArray(ret);return ret;}    return null;}}



0 0
原创粉丝点击