字符串之字形排列
作者:互联网
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, ]
标签:colIndex,排列,int,chars,isAsc,char,之字形,字符串,col 来源: https://www.cnblogs.com/dongma/p/12730293.html