映射看起來直截了當:
target[x, y] = source[OFFSET - y, x]
其中OFFSET
是最後行的索引拷貝(33
如果我算右)。
你的實現可以使用兩個嵌套循環來複制數組。
編輯:
這是你的轉換可能是什麼樣子:
public class Converter
{
public static boolean[][] convert(boolean[][] source, int offset, int width)
{
final boolean[][] target = new boolean[width][source.length];
for(int targetRow=0 ; targetRow<width ; targetRow++)
{
for(int targetCol=0 ; targetCol<source.length ; targetCol++)
{
target[targetRow][targetCol] = source[targetCol][offset + width-1 - targetRow];
}
}
return target;
}
}
這是下面的測試代碼(原陣列和轉換陣列)的輸出使用偏移量爲2
(前兩行省略)和寬度爲7
(轉換了7列):
█
███
██ █
██ ████
██ █ █
██ ████ ███
█ █
██
█ █ █
██ ███
██ █
██ █
██
測試代碼是源陣列的方向,內容和輸出字符串轉換定義:
public class ConverterTest
{
private final static int OFFSET = 2;
private final static int WIDTH = 7;
private static final String[] sourceString = {
" █ ",
" ███ ",
" ██ █ ",
" ██ ████ ",
" ██ █ █ ",
"██ ████ ███",
};
public static void main(String[] args)
{
final boolean[][] source = getSourceArray();
printArray(source);
final boolean[][] target = Converter.convert(source, OFFSET, WIDTH);
printArray(target);
}
private static boolean[][] getSourceArray()
{
final boolean[][] sourceArray = new boolean[sourceString.length][sourceString[0].length()];
for(int row=0 ; row<sourceString.length ; row++)
{
for(int col=0 ; col<sourceString[0].length() ; col++)
{
sourceArray[row][col] = (sourceString[row].charAt(col) != ' ');
}
}
return sourceArray;
}
private static void printArray(boolean[][] arr)
{
for(int row=0 ; row<arr.length ; row++)
{
for(int col=0 ; col<arr[0].length ; col++)
{
System.out.print(arr[row][col] ? '█' : ' ');
}
System.out.println();
}
System.out.println();
}
}
對不起,我不明白你的答案。你能否進一步闡述(x,y和OFFSET代表什麼)。我讀了你的抵消定義,我真的不明白這一點。 – liamzebedee 2012-03-28 10:11:18
@ LiamE-p:編輯我的答案,包括一些示例代碼。希望能解釋我的意思。 – 2012-03-28 18:47:33
@ LiamE-p:我的回答有幫助嗎? – 2012-04-06 09:09:47