wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes New ArrayList Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New ArrayList Problem" Watch "New ArrayList Problem" New topic
Author

New ArrayList Problem

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
I have to questions regarding ArrayList
1. the get method has the signature as follows:
Object get(int index)
Why is it not neccessary to cast Object to integer
like (System.out.println((Integer)lst.get(i-1))?
Why ist lst.set(5, new Integer(1000) setting the 1000 to 6th
element and not to 5th element?


Interesting is that when I want to calcultate with lst.get(i-1) like shown in the code
As follows then I get this error:

C:\Java\EigeneJavaProgramme>javac TestArrayList.java
TestArrayList.java:24: operator + cannot be applied to int,java.lang.Object
sum += lst.get(i-1);
^

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Thomas Markl:
1. the get method has the signature as follows:
Object get(int index)
Why is it not neccessary to cast Object to integer
like (System.out.println((Integer)lst.get(i-1))?

Because there is a System.out.println(Object o) method, which will call String.valueOf(o), which will polymorphically call o.toString() on non-null objects. Therefore, in the above case, the return value of Integer.toString() will be printed, which is exactly what you want.

Why ist lst.set(5, new Integer(1000) setting the 1000 to 6th
element and not to 5th element?

Because indices of Lists (as well as arrays) are zero-based.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: New ArrayList Problem
 
Similar Threads
Problem in arraylist
Divide a vector
Why I get the List content when I print the List object?
ArrayList Problem
Is StringTokenizer a "Java Gotcha" for use in CSV processing?