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

Thread: URGENT Array List Question

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default URGENT Array List Question

    Suppose that you have an ArrayList and that it contains String objects.
    Which declaration of the ArrayList requires that objects retrieved using the
    get method be cast to Strings before calling a String method?

    I. ArrayList a = new ArrayList();
    II. ArrayList<Object> a = new ArrayList<Object>;
    III. ArrayList<String> a = new ArrayList<String>;

    A. I only
    B. II only
    C. III only
    D. I and II only
    E. I, II, and III


    I really have no idea on this question. I know that all of these are ways to declare an Array List, but I am unfamiliar with the last two since I usually just declare my Array Lists with the first option.


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

    Default Re: URGENT Array List Question


  3. The Following User Says Thank You to copeg For This Useful Post:

    iWaterBuffalo (May 5th, 2014)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: URGENT Array List Question

    Okay, so I know that I is definitely correct. So that brings me down to A or D. Would <Object> behave the same as <String>?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: URGENT Array List Question

    Your last question is a good one. If the answer is not obvious after about 15 seconds of thought, you could write a simple program to see if calling a String method with an Object is the same as calling a String method with a String instance.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    iWaterBuffalo (May 5th, 2014)

  7. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: URGENT Array List Question

    Thank you both. I figured out the correct answer.

  8. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: URGENT Array List Question

    Only III is correct. Even though you may use I & II to create a String ArrayList, you still need to cast them before using.

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

    Default Re: URGENT Array List Question

    Quote Originally Posted by PitLv View Post
    Only III is correct. Even though you may use I & II to create a String ArrayList, you still need to cast them before using.
    Is that your final answer?

    Which declaration of the ArrayList requires that objects retrieved using the
    get method be cast to Strings before calling a String method?

Similar Threads

  1. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  2. checking array list items!!! urgent help please
    By hacikho in forum Java Theory & Questions
    Replies: 1
    Last Post: December 14th, 2011, 04:28 PM
  3. help with array assignment ! urgent please :(
    By dre2327 in forum Collections and Generics
    Replies: 5
    Last Post: October 6th, 2011, 01:30 PM
  4. Array List question
    By Demetrius82 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 3rd, 2011, 10:28 AM
  5. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM

Tags for this Thread