arrays - byte[] linked list in java -


i trying create linked list witch contains byte arrays:

static queue<byte[]> q = new linkedlist<byte[]>(); static byte buf[] = new byte[1024];  static void queueinit() throws ioexception{     bytearrayinputstream bis= new bytearrayinputstream(buf);     datainputstream ois= new datainputstream(bis);      randomaccessfile myfile = new randomaccessfile ("keys", "rw");     for(int r=0;r<90;r++){         myfile.seek(r*1024);         myfile.read(buf);         q.add(buf);     }     myfile.close(); } 

the problem @ commant q.add(buf);. buffer loaded correct bytes list loading zeros.any help?:)

no, linkedlist have several references same array. you're ever creating 1 byte array - , reading data file time after time.

it's not clear why you've declared buf static variable @ all, need create new array each element in list:

for (int r=0;r<90;r++) {     byte[] buf = new byte[1024];     myfile.seek(r * 1024);     myfile.read(buf);     q.add(buf); } 

also note you're ignoring return value of read(), indicates how many bytes have been read. might not have read 1024 bytes... want in case?

(also, why bother seeking, or indeed using randomaccessfile? if want read first 90k in 1k chunks, can sequentially, no seeking @ all.)


Comments