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);
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.
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);
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!
.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 09:32 AM.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
I do not understand.
Try thinking -.-
Now, with what I also said in mind, what index would your value '33' correspond to?The get(int) function returns the value at that index
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
Object obj = arrayList.get(33);
I tried that and it did not work. I feel completely lost.
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 08:03 PM.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your 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.
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!
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.
.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.
The index of the first value would be zero right?
yep (or you can try it out and see).
|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