我仍然有問題複製到另一個陣列的一部分。我試圖檢查數組的內容,以確保事情正常運行,但所有東西打印爲零。我首先將我的數組初始化爲零,當程序發生時,將不同的部分複製到名爲dataBlock的數組中,這是數組的一部分,稱爲緩存,它由名爲SlotNodes的對象組成。複製陣列的一部分到另一個 - Java
東西都在我的模擬類初始化爲這樣
public static SlotNode[] cache = new SlotNode[8];
public static int cacheSize = 8;
public static int memSize = 2048;
public static byte[] main_Mem = new byte[memSize];
有一個菜單,如果我們試圖讀取的地址,我們解剖地址:
public static void readAddress() {
System.out.println("What address? ");
//String tmpAddress = keyboard.next();
address = keyboard.nextInt(16);
System.out.println("After parsing the string to base 16, you get ");
System.out.printf("%X", 0xFF & address);
System.out.println(" ");
//get offset
offset = address & 0x7;
System.out.println(offset);
//get tag
int tmpTag = address >> 6;
tag = tmpTag & 0x1F;
System.out.println(tag);
//get slot
slot = (address >> 3) & 0x7;
System.out.println(slot);
//go to slot number and see what valid bit is
if (cache[slot].getValidBit() == 0) {
當validbit爲0,那麼我們必須將main_Mem數組中的數據塊複製到緩存數組的dataBlock數組中。
System.out.println("Miss");
//copy block in main memory, 8 bytes
//address/8 gets address of block in mainmemory
int startAddress = address & 0x7F8;
System.out.println("The start address of " + address + " should be " + startAddress);
cache[slot].setValidBit(1);
cache[slot].setTag(tag);
//Now need to copy from startAddress plus 8 bytes into cache[slot]
while (cacheSize < 8){
System.arraycopy(main_Mem, startAddress, cache[slot].dataBlock, 0, cacheSize);
}
System.out.println();
System.out.println("The block of data is: ");
for (int i=0; i<8; i++) {
for (int j=0; j<7; j++){
System.out.println("Value: " + cache[i].dataBlock[j]);
}
}
}
我設置爲'1'的有效位打印出來很好。它看起來像main_mem數組中的數字沒有被複制到他們需要的地方。 :(
不應該「while(cacheSize <8){」be「if(cacheSize <8){」以防止無限循環? – 2012-03-22 00:41:33
已更新但所有東西仍然打印爲零 – jackie 2012-03-22 00:44:00
忘記我以前的評論... – Jochen 2012-03-22 00:49:34