Tuesday, June 9, 2009

accumulator

I would need to scan a directory, take note of each of the file size, and do a count on how many files there are with a particular size.

file size is long
count is int

so i have one array to keep track of file size
another array to keep the int count

so once i have a file size, i would check through the array of file size, to see if it is the same, if yes, i remember the index and take the value from the count array plus one and put back in. The data structure which i used initially was 2 arraylist.

for sizeArray
  if sizeArray[i] is filesize
    take countArray[i]+1
    addedFlag to false

if addedFlag
  sizeArray[max] is filesize
  countArray[max] is 1

it looks awfully complicated, so i changed the 2 array into a hashtable
the long value will be converted to a string object.

if hashtable has fileSizeString
  currentCount is hashtable get fileSize
  hashtable put fileSizeString, currentCount+1

hence making the code a lot more shorter.

No comments:

Post a Comment