Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: is there a better way to extend an array?

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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).
    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?

    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.

    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

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    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.

    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());

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: is there a better way to extend an array?

    That seem pretty simiple. So instead of

    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.
    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

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 01:01 PM
  2. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 12:54 PM
  3. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  4. problem using the "extend"
    By Eildotcom in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 06:54 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM