java - Get information outside a loop -


i've problem. want fill array objects containing different informations. here loop

public filerecord [] calcpos() throws ioexception{         (int = 0; < getefsfatmaxrecords(); i++){         int blocknumber = i/5;         int recordoffset = i%5;         pos = (recordoffset*100+(getfsatpos() + 512 + 512*blocknumber));          filerecord rec = new filerecord(pos,getheader());         array = new filerecord[header.getmaxfilerecords()];          array[i] = rec;         system.out.println("filename: " + array[i].getfilename());               }         return array;     } 

it should make different objects of filerecord. position depends on running variable i. t loop stores in array , returns array. ive declared array global variable in calss thought changes inside loop directly affect global array. doesnt work. i'm doing wrong?

you re initializing array in every iteration. below correct version of code want:

public filerecord [] calcpos() throws ioexception{      filerecord[] array = new filerecord[header.getmaxfilerecords()];       (int = 0; < getefsfatmaxrecords(); i++){        int blocknumber = i/5;        int recordoffset = i%5;        pos = (recordoffset*100+(getfsatpos() + 512 + 512*blocknumber));         filerecord rec = new filerecord(pos,getheader());        array[i] = rec;        system.out.println("filename: " + array[i].getfilename());     }     return array; } 

as vogel says if header.getmaxfilerecords() changes within loop array may run out of bound.

solution: arraylist should work.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -