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 15 of 15

Thread: Please help ArrayList Question

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help ArrayList Question

    Declare an ArrayList named taxRates of * five *elements of type Double and initialize the elements (starting with the first) to the values 0.10 , 0.15 , 0.21 , 0.28 , 0.31 , respectively.


    I tried:

    double taxRates[5] = {0.10, 0.15, 0.21, 0.28, 0.31};


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help ArrayList Question

    That's an array, not an ArrayList.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help ArrayList Question

    What does that mean?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help ArrayList Question

    An ArrayList is an Object that holds a variable amount of other Objects.

    An array is a container that holds a fixed number of Objects or primitives.

    They aren't the same, and what you've supplied doesn't match your title, so some clarification is necessary.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help ArrayList Question

    Additional to Kevin's answer: Well, in easy words though
    1. Arrays are non resizable
    2. Arrays store only single data type values(Say Elements).
    3. ArrayLists are resizable.
    4. ArraLists can store multi data type values(Say Objects).

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help ArrayList Question

    Quote Originally Posted by Mr.777 View Post
    Additional to Kevin's answer: Well, in easy words though
    1. Arrays are non resizable
    2. Arrays store only single data type values(Say Elements).
    3. ArrayLists are resizable.
    4. ArraLists can store multi data type values(Say Objects).
    I just woke up and I haven't tested this, but couldn't you store multiple types in an array by doing something like this:

    Object[] objs = {new Object(), "cats", 7};
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help ArrayList Question

    Well, arrays of primitive types, not of some defined objects.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help ArrayList Question

    Quote Originally Posted by Mr.777 View Post
    Well, arrays of primitive types, not of some defined objects.
    I'm still not sure I agree with that. Arrays definitely can contain primitives (whereas ArrayLists can only contain Objects, including wrapper classes for primitives). But arrays can also contain Objects, like I showed above, right?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help ArrayList Question

    Quote Originally Posted by KevinWorkman View Post
    I'm still not sure I agree with that. Arrays definitely can contain primitives (whereas ArrayLists can only contain Objects, including wrapper classes for primitives). But arrays can also contain Objects, like I showed above, right?
    int [] arr = new int[10];
    What do you say about this?

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help ArrayList Question

    Quote Originally Posted by Mr.777 View Post
    int [] arr = new int[10];
    What do you say about this?
    Like I said, arrays definitely CAN contain primitives, but they don't HAVE to. They con contain Objects as well. So to say the can only contain a single data type is not correct. I don't want to be pedantic, but the OP seems extremely confused already, so I don't want to confuse him even more.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Mr.777 (November 15th, 2011)

  13. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help ArrayList Question

    Oh yeah if this is the fact then yes arrays can have multiple types. So, ignore the point, Arrays can store only single type elements.

    So Kevin, don't you think, elements and objects are different things?

  14. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please help ArrayList Question

    Quote Originally Posted by Mr.777 View Post
    Oh yeah if this is the fact then yes arrays can have multiple types. So, ignore the point, Arrays can store only single type elements.

    So Kevin, don't you think, elements and objects are different things?
    It depends what you mean by "elements". Objects can be elements in an array or an ArrayList. But if you mean "primitives" instead of "elements" then yeah, Objects and primitives are different.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Please help ArrayList Question

    Yes that is fine. What Kevin is questioning is the following statement made by you:

    2. Arrays store only single data type values

    Which is incorrect! Arrays can most certainly hold elements of different data types as long as they are subclasses of a superclass. Just as Kevin demonstrated above

    Bah! Page 2.
    Improving the world one idiot at a time!

  16. The Following 2 Users Say Thank You to Junky For This Useful Post:

    KevinWorkman (November 15th, 2011), Mr.777 (November 15th, 2011)

  17. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help ArrayList Question

    Alright Kevin. Thanks. Well, atleast i got some new knowledge coz of you.
    Thanks alot.
    Thanks Junky.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM