2011-12-04 44 views
0

我有一個ShortBuffer的問題。這是我的代碼:ShortBuffer和Java中的矩陣

FileChannel fc = new FileInputStream("C:/Dane DMS/"+names2).getChannel(); 
ByteBuffer bb = ByteBuffer.allocateDirect((int) fc.size()); 
while (bb.remaining() > 0) fc.read(bb); 
fc.close(); 
bb.flip(); 
// choose the right endianness 
ShortBuffer sb = bb.order(ByteOrder.BIG_ENDIAN).asShortBuffer(); 

在這個文件中我有一個矩陣。

111 222 333 123 
444 555 666 456 
777 888 999 789 
098 765 432 321 

我需要這個矩陣更改爲:

098 765 432 321 
777 888 999 789 
444 555 666 456 
111 222 333 123 

我必須改變這種矩陣或創建循環,從098號開始,並在123號結束

我不打印這個矩陣。我使用的是:

for(int i = 0; i<=1200; i++) 
        { 
         for(int j = 0; j<=1200 ; j++) 
         { 

         } 

        } 

到交叉矩陣,但這種方式我是從111號開始,我需要在123

+0

哪裏是你用它來打印矩陣代碼,什麼是秩序矩陣中的元素:從左到右,然後從上到下或不同的? –

回答

1

你可以試試這個

FileChannel fc = new FileInputStream("C:/Dane DMS/"+names2).getChannel(); 
ByteBuffer bb = ByteBuffer.allocateDirect((int) fc.size()); 
while (bb.remaining() > 0) fc.read(bb); 
fc.close(); 
bb.flip(); 
// choose the right endianness 
ShortBuffer sb = bb.order(ByteOrder.BIG_ENDIAN).asShortBuffer(); 

short[][] Matrix = new short[1201][1201]; 
for(int i = 0; i<=1200; i++) 
{ 
    for(int j = 0; j<=1200 ; j++) 
    { 
     Matrix[1200-i][j] = sb.get(i*1201+j); 
    } 
} 
+0

謝謝。這非常有幫助。 – edi233

0

所以你需要扭轉的行從098和月底開始?要做到這一點,你需要計算出行長。然後你就可以交換第一和最後一行,則倒數第二排和第二排等

+0

嗯,你能告訴我如何在這個矩陣上做到這一點?實際上這個矩陣將是1201x1201。 – edi233