char[] stringArray=str.toCharArray()
double n=(double)Math.sqrt(str.length())
int num=(int)Math.ceil(n)
char square[][]=new char[num][num]
int i=0
for(int row=0row <num &&i <str.length()row++) {
for(int column=0column <num &&i <str.length()column++) {
square[row][column]=stringArray[i]
System.out.print(square[row][column] + " ")
i++
}
System.out.println("")
}
你的这个逻辑有问题,不要最外面那个for了
public class{
public static void main(String[]args){
String s="1,2,3,4,5,6,7,8,9"/定义变量s/
int[][] a=new int[3][3]/定义数组a/
String[]b=s.split(",")/类型转换/
for(int i=0,j=0,k=0i)/for语句,定义i,j,k,表数组样式/
a[j][k++]=Integer.parseInt(b[i])
if(k==3){j++k=0}
}
//打印出来
for(int[]c:a){
for(int n:c){
System.out.print(n+" ")/输出/
}System.out.println()
}
}
}
// 都当做字符串String[][] arr = { { "a", "b", "cd", "e efg" }, { "1", "2" } }
// 或者定义为Object类型数组
Object[][] brr = { { "a", "b", "cd", "e efg" }, { 1, 2 }, { 'a', 'b' } }