Finding biggest files, making list
Okay, I know my issue is in my add function [adds to the array]
so I'll only post that portion of the code
Code :
public void add(Comparable<Wrapper> c)
{
for(int x =0; x<CompFiles.length ; x++)
{
if(CompFiles[x] == null )
{
CompFiles[x] = c;
return;
}
}
for(int x = 0; x<CompFiles.length; x++)
{
int stat = c.compareTo((Wrapper) CompFiles[x]);
if(stat > 0)
{
CompFiles[x]=c;
return;
}
}
}
Oh and the compareTo code
Code :
@Override
public int compareTo(Wrapper o)
{
return (int) (this.size - o.getSize());
}
Which is in the Wrapper class.
Now my best explaination of the bug
"
It works great up to a list of 3, or if the list can fit all the files.
Other than that, I was trying it with a directory with 16 files, the first 3 were correct, but the 4th on the list was actually the 7th biggest in the directory -- [using a list of 4].
I can't figure out the bug for the life of me.
"
[sent to my teacher along with my code, YES IT IS A COLLEGE PROJECT, but I ALREADY HANDED IT IN... even though I handed it in already -- I wanna figure out whats wrong with it]
Re: Finding biggest files, making list
If you want the entire source for compilation purposes, let me know and I'll post it.
Re: Finding biggest files, making list
Well I figured out what the problem was, my adding after filling up the array was flawed
Code :
for(int x = 0; x<CompFiles.length; x++)
{
int stat = c.compareTo((Wrapper) CompFiles[x]);
if(stat > 0)
{
CompFiles[x]=c;
return;
}
}
Bascially say I have the 2nd largest, 3rd largest, and 4th largest filled up the array of 4. But I have 5 files. I find the 5th file, check the [0] of the array first, which would be the 2nd largest, and figure OH LOOK ITS LARGER and throw that out.
So I had to find the smallest in that array, adn then check to see if its bigger than the file I'm looking to add.
Code :
public void add(Comparable<Wrapper> c)
{
for(int x =0; x<CompFiles.length ; x++)
{
if(CompFiles[x] == null )
{
CompFiles[x] = c;
return;
}
}
int smallestFileIndex=0;
long smallestFileSize=((Wrapper)CompFiles[0]).getSize();
for(int x=0; x<CompFiles.length; x++)
{
if(((Wrapper)CompFiles[x]).getSize()<smallestFileSize)
smallestFileIndex=x;
}
if(((Wrapper)CompFiles[smallestFileIndex]).getSize()<((Wrapper) c).getSize())
CompFiles[smallestFileIndex]=c;
}
As you see, I just had to add a check thats it.