我正在开发一个项目,需要在 Android 中将字符转换为二维数组。例如,如果我有字符(或字符串)“A”,我必须创建一个二维数组,如下所示:
[0,0,1,0,0]
[0,1,0,1,0]
[0,1,0,1,0]
[1,0,0,0,1]
[1,1,1,1,1]
[1,0,0,0,1]
[0,0,0,0,0]
图形化:
我该如何处理这个问题?
最佳答案
您可以尝试以下操作:
String str = "ABC";
List<byte[][]> codedString = new ArrayList<byte[][]>();
for ( char c : str.toCharArray() ) {
switch ( c ) {
case 'A':
codedString.add( new byte[][]{
{ 0, 0, 1, 0, 0 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0 } } );
break;
case 'B':
codedString.add( new byte[][]{
{ 1, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 0 } } );
break;
case 'C':
codedString.add( new byte[][]{
{ 0, 1, 1, 1, 0 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 1, 1, 0 } } );
break;
}
}
for ( byte[][] codedChar : codedString ) {
for ( int i = 0; i < codedChar.length; i++ ) {
for ( int j = 0; j < codedChar[i].length; j++ ) {
System.out.print( codedChar[i][j] == 1 ? "X" : " " );
}
System.out.println();
}
}
您需要对所有模式进行硬编码。
编辑:我更改了代码以使用零和一来表示模式。它比使用 boolean 值更容易阅读。
关于java - Android中如何将字符转换为 boolean 二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241953/