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: I am having a hard time with my code

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

    Default I am having a hard time with my code

    Given an ArrayList a , write an expression that refers to the first element of the ArrayList. So, if the array a held the following values: 33,14,97,265, 84, then the value of your expression should be 33.

    I tried:
    Object obj = arrayList.get(33);


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

    Default Re: I am having a hard time with my code

    Check out the API for ArrayList. The get(int) function returns the value at that index. Index 33 doesn't exist.
    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
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I am having a hard time with my code

    .get(int) refers to the Arraylist index, not the value of the ArrayList object.

    Note:
    ArrayList indices start off from 0, just like arrays.

    Edit: Too slow
    Last edited by newbie; November 12th, 2011 at 08:32 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: I am having a hard time with my code

    I do not understand.

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I am having a hard time with my code

    Try thinking -.-

    The get(int) function returns the value at that index
    Now, with what I also said in mind, what index would your value '33' correspond to?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: I am having a hard time with my code

    Object obj = arrayList.get(33);
    I tried that and it did not work. I feel completely lost.

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I am having a hard time with my code

    An ArrayList which contains values: 2, 4, 6, 8 has a size of 4.
    get(int) method returns the value @ the specified index.

    ArrayList indices start from 0.
    In this case, all the indices will range from 0:3.

    Can't make it clearer without coming to your house and making Kevin's cat type it for you

    Oh yeah: be sure to check out Oracle's Java Tutorials - starting with the basics.
    Last edited by newbie; November 11th, 2011 at 07:03 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: I am having a hard time with my code

    Thanks, I guess I'm a bit slow because I still do not understand. I appreciate your time and response. I am new in this class and your respnse is so foreign to me. I am in desperate need of a tutor and am trying my hardest to complete this question as well as understand what I am doing. Have a great day/night.

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

    Default Re: I am having a hard time with my code

    Like newbie said, if you're in that dire of straits, you need to start at the basic tutorials.

    And my cat is not helpful when it comes to keyboards or mice.

    stanleyMouse.jpg

    See?
    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
    Junior Member
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am having a hard time with my code

    I guess everyone understand but me. First and foremost, I am allergic to cats, secondly, please pardon me if I don't get the joke, all I know is that my professor is located in Germany and I am alone in this sinking ship, be well.

  11. #11
    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: I am having a hard time with my code

    .get() gets the element referenced by the index. You're putting the value into get(), not the index.

    In you array you have the following:

    index: value
    0: 33
    1: 14
    2: 97
    3: 265
    4: 84

    What is the index of the first item? Hint: it's not 33.

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

    Default Re: I am having a hard time with my code

    The index of the first value would be zero right?

  13. #13
    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: I am having a hard time with my code

    yep (or you can try it out and see).

  14. #14
    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: I am having a hard time with my code

    |32|12|31|33|

    Let's say it's an array named Arr.
    It's size is 4, why? Coz it has four elements(Values) in it.
    How do you get values? Like;
    Arr[0]; this will be 32
    Arr[1]; this will be 12
    And what if you will try to do;
    Arr[33]; As 33 index doesn't exist, why this index doesn't exist? Coz your array size is only 4, so it makes it's index limited to 0:3. Now this statement will throw ArrayOutOfBoundException.

    I don't know if you still catch this or not else invite us at home to teach you in much better way Kidding

  15. #15
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I am having a hard time with my code

    Quote Originally Posted by KevinWorkman View Post
    And my cat is not helpful when it comes to keyboards or mice.
    stanleyMouse.jpg

    See?
    Haha!
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Simple code apparently too hard for me...
    By Avopeb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 02:35 PM
  2. [SOLVED] New to java, need to know how to incorparate system time into a block of code.
    By lostbit in forum Java Theory & Questions
    Replies: 5
    Last Post: September 29th, 2011, 07:46 AM
  3. Having a hard time figuring out how to make my code work
    By iainnitro in forum Loops & Control Statements
    Replies: 2
    Last Post: September 6th, 2011, 07:48 AM
  4. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  5. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 01:51 AM