Results 1 to 8 of 8

Thread: List or Array

  1. #1
    wiz6 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default List or Array

    What return type(List or Array) should i use if i want take all rows in database table and after that pass the Array or List into JSP page?

    For example:
    public Item[] retrieveAll();
    or
    public ArrayList<Item> retrieveAll();

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: List or Array

    What happened when you tried each?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: List or Array

    They will both work just fine. I'd say the trouble may more be in constructing that list or array initially. A list has the benefit of being able to grow, while an array cannot. That makes a list far easier to work with.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: List or Array

    Quote Originally Posted by gimbal2 View Post
    They will both work just fine. I'd say the trouble may more be in constructing that list or array initially. A list has the benefit of being able to grow, while an array cannot. That makes a list far easier to work with.
    Easier to work with perhaps, but a List has way more overhead since it resizes as it grows. This takes processing time during the building of the List and will require more memory since you'll end up with an internal array that's much larger than the elements it actually contains. This might be a big deal or it might not be, but saying that one is easier than the other doesn't paint the whole picture.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: List or Array

    No, but I don't worry about performance until I have a performance problem. I've never ever seen any kind of noticable impact from using a list, computers are just too fast to still be making such considerations.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: List or Array

    Quote Originally Posted by gimbal2 View Post
    No, but I don't worry about performance until I have a performance problem. I've never ever seen any kind of noticable impact from using a list, computers are just too fast to still be making such considerations.
    I can mostly agree with you, especially for desktop computing. But this is JSP so it's presumably happening on a server where resources are more expensive. Probably not enough to notice, but it's definitely something to keep in mind.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    wiz6 is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: List or Array

    Okay, my second problem is how to i get data into the Object array from database.
    My code which doesnt work:

    Item[] allItems = null;
    int count = 0;
    //reading data from table
    while(result.next()){
    Item item = new Item();
    item.setItem( result.getInt("item") );
    item.setName( result.getString("name") );
    items[count] = item;
    ++count;
    }

    If i use Lists, then its all Okay. I hope you understand me. :)

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Array List
    By turtlish in forum New To Java
    Replies: 10
    Last Post: 12-20-2011, 08:48 PM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array List help
    By Weejee37 in forum New To Java
    Replies: 4
    Last Post: 10-27-2009, 12:32 AM
  5. array list help
    By dorno83 in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 09:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •