is there a better way to extend an array?
I have a database which has 1000s of files stored.
I need to access a table and store all the values into a dropdown list for a user to view.
Currently i set the array size to 1 and using an sql statement i pull back the data and store it into an array.
im just wondering if theres a better and more efficient way of extending an array because im using arraycopy and for the amount of files i have, takes quite awhile.
Thanks
Kurt
Re: is there a better way to extend an array?
Try using the ArrayList class. It still "extends" arrays by copying the array, but it's considered O(1) average because it doesn't extend it 1 element ad a time (rather, I believe it doubles it's capacity when it needs to).
Also, why do you need to pull all 1000 files if you just need the file names to be displayed? 1000 file names is always smaller in size than 1000 files.
Additionally, if you know you're going to have a large number of files, you can specify the initial capacity of the ArrayList to be quite large.
Re: is there a better way to extend an array?
Quote:
Try using the ArrayList class. It still "extends" arrays by copying the array, but it's considered O(1) average because it doesn't extend it 1 element ad a time (rather, I believe it doubles it's capacity when it needs to).
Il have a look at that. Iv never used array list though. can you access them the same as an array? and if it doubles in size would that not leave memory wasted?
Quote:
Also, why do you need to pull all 1000 files if you just need the file names to be displayed? 1000 file names is always smaller in size than 1000 files.
Sorry yes i meant file names.
Quote:
Additionally, if you know you're going to have a large number of files, you can specify the initial capacity of the ArrayList to be quite large.
I need it to be generic as the amount of files grows daily.
Using mysql i get the data using resultset and i had a look but is there a way to get the size of the result?
My other way of doing this was to query the database twice with the same code.
First - an integer increaments to find the number of files
Second - to add the filename the an array
But thought extending the array would be better and more efficient.
Am I correct?
Thanks
Kurt
Re: is there a better way to extend an array?
Extending an array could be more efficient, but unfortunately due to the nature of modern computer's memory management, it's not possible.
Yeah you do have some wasted space, but consider this:
You have an ArrayList with a capacity of 2000, but it only has 1000 elements. The ArrayList by itself will take up ~8000 bytes, of which ~4000 bytes will actually be used (note: the data being stored is not being counted, it's stored somewhere else in memory and is purely dependent on only the actual items you have, not the capacity. I'm also assuming a 32-bit system using 32-bit pointer sizes). Most modern computers have 1-8 GB of memory, with an average of ~2 GB (this number could actually be higher, likely 3GB for 32-bit systems and 4-6 GB for 64-bit systems). You're wasting about 0.0000018% of your computer's memory by having the extra capacity. Also, with ArrayLists the idea is that you're capacity should allow the ArrayList to grow while keeping the actual memory footprint to stay relatively close to what's actually needed. So if you were to add another 900 elements to that ArrayList, it's size would roughly match that of the array which contains 1900 elements.
Quote:
My other way of doing this was to query the database twice with the same code.
First - an integer increaments to find the number of files
Second - to add the filename the an array
I've found that more often than not it's the network that's the bottleneck rather than the program (I'm assuming you're using SQL to connect to some server) so the less you need to read from the server the faster your program would be. Using an ArrayList means you have to send half the number of requests to the server in order to generate the list of file names. The difference in performance between the two methods could be very similar (and thus negligible), though, depending on what your connection speed is and exactly how many files you have.
Using ArrayLists are somewhat different from using arrays (you have to actually call the method), but the concept is very similar.
Code Java:
ArrayList<String> filenames = new ArrayList<String>();
filenames.add("test.txt");
filenames.add("test1.txt");
filenames.add("test2.txt");
filenames.add("test3.txt");
System.out.println("file 0: " + filenames.get(0));
System.out.println("number of files: " + filenames.size());
Re: is there a better way to extend an array?
That seem pretty simiple. So instead of
Code :
String[] files = new String[1];
int i = 0;
while (rs.next())
{
files[i] = rs.getString("Filename");
files = extend(files, files.length+1);
}
I can use.
Code :
ArrayList<String> files = new ArrayList<String>();
while (rs.next())
{
files.add(rs.getString("Filename"));
}
Correct me if im wrong there but i think i get the basics. And i also dont need to declare the size of the arrayList??
I think I'l use an arraylist instead. and yes the network connection here is bad so i wont be querying the Db twice.
Thanks for your help.
Kurt
Re: is there a better way to extend an array?
That is correct.
If you want the specifics of ArrayList:
ArrayList is one of many dynamically sized Datastructure Objects that extend the List Object (List (Java Platform SE 6)). The main difference between List Objects and an Array is that an Array is just a standard allocation of memory while a List (an ArrayList in this case) is an Object. That is also the reason that lists are interacted with using methods instead of just references (or whatever they are called).
Here is a quick reference for List methods vs. Array references:
List.size() instead of Array.length
List.get(0) instead of Array[0]
List.set(0,"Word") instead of Array[0] = "Word"
And some of the more advanced features, such as removing an Object is a simple method call in a List instead of a complete Array reallocation.
If you wanted to add to the end of an Array, you would have to copy, reallocate, ect. With a List, you simply say List.add("Word")
If you wanted to remove in the middle of an Array, you would have to shift elements down and resize. With a List, you simply say List.remove(4)
And, if absolutely necessary, you can convert a List to an Array with the List.toArray() method.
Since ArrayList extends List, ArrayList has a ton more features than the List class and you should have a look at the ArrayList API to see if it can do what you want: ArrayList (Java Platform SE 6)