import java.util.*; public class Client { public static void main(String[] args) { String str = "abcdefghijk"; char[][] chars = strArr(str, 4); for (char[] ints : chars) { System.out.println(Arrays.toString(ints)); } } /** * 字符串排列 */ static char[][] strArr(String str,int col) { char[] chars = str.toCharArray(); int row = (chars.length - col) / (col - 1) + 2 ; char[][] arr = new char[row][col]; int rowIndex = 0; int colIndex = 0; int charIndx = 0; boolean isAsc = true; while (charIndx < chars.length){ arr[rowIndex][colIndex] = chars[charIndx]; charIndx++; if(isAsc){ colIndex++; }else { colIndex--; } if(isAsc && (colIndex >= col)){ isAsc = false; colIndex -= 2; rowIndex++; }else if(!isAsc && (colIndex < 0)){ isAsc = true; colIndex += 2; rowIndex++; } } return arr; } }
结果
[a, b, c, d] [g, f, e, ] [ , h, i, j] [ , , k, ]