Computer Question (intermediate)-re-split the strings in the array by the specified length (Java)
The questions are as follows:
The Code is as follows:
Package huawei; import java. util. arrayList; public final class Demo {/** function: compile a function and input it as a string array. * split each string in the array according to the specified length iInputLenth, output to the new string array. If the length of a string is not an integer multiple of iInputLenth, add 0 to the end. * Null strings are not processed. If an empty string is encountered, the array ends and the function returns the result. Input: String inputStrArray String Array pointer * the maximum number of strings is 50, and the maximum length of a String is 255. The iInputLenth Length Specified by int iInputLenth is iInputLenth >=1 & <= 32 **. Return: string Array pointer ** example split by length 8 Input: abc 12345789 return: abc00000 12345678 90000000 */public static String [] convertStringArray (String [] inputStrArray, int iInputLenth) {if (iInputLenth> = 1 & iInputLenth <= 32) // split length {ArrayList
ArrayList = new ArrayList
(); For (int I = 0; I <inputStrArray. length; I ++) {String srcString = inputStrArray [I]; if (srcString. length ()> 0) // not empty {char src [] = srcString. toCharArray (); // converts a string to an array of characters int len = src. length; // String Array length StringBuilder sb = new StringBuilder (); boolean isContinue = true; int ring = 1; // used to clear the count for (int index = 0; isContinue; index ++, ring ++) {if (index = (len-1) // The length and Index are both {sb. append (src [index]); int tmp_len = sb. length (); for (; tmp_len <iInputLenth; tmp_len ++) {sb. append ("0"); // Add zero} arrayList. add (sb. toString (); isContinue = false; // do not continue} else {sb. append (src [index]); // convert the character array to if (ring = iInputLenth) // if the length is equal to the split length, split it once and save sb to the list, and clear sb, the next round starts {ring = 0; arrayList. add (sb. toString (); sb = new StringBuilder () ;}}} if (arrayList. size ()> 0) {String data [] = new String [arrayList. size ()]; data = arrayList. toArray (data); // convert the List into a string array return data ;}} return null ;}}