A question about understanding arraylist and object

T

Tony Johansson

Hello!

Assume I create an ArrayList like this
ArrayList arraylist = new ArrayList();
Now to my questions:
1. Does this mean that all the elements that is added to the arraylist is of
type object and is referencing the actual added elements ? Is this correct?


2.So assume I have three elements in the arraylist consisting of three
different types.
The first added element to arraylist is a class type which is called
clock( Reference type)
The second added element is value type for example an int which is boxed.
(Value type)
The third added element is string.(Reference type)
So now I have three elements of type object in the arraylist. I call these
three element
object 1, object2 and object3
Now object1 is referencing the clock object
object2 is referecing the boxed int
object3 is referencing a string object.

Can you just correct me if I'm wrong at some point in the above description
?

//Tony
 
B

Bill Butler

Tony Johansson said:
Hello!

Assume I create an ArrayList like this
ArrayList arraylist = new ArrayList();
Now to my questions:
1. Does this mean that all the elements that is added to the arraylist
is of type object and is referencing the actual added elements ? Is
this correct?
Correct.

2.So assume I have three elements in the arraylist consisting of three
different types.
The first added element to arraylist is a class type which is called
clock( Reference type)
The second added element is value type for example an int which is
boxed. (Value type)
The third added element is string.(Reference type)
So now I have three elements of type object in the arraylist. I call
these three element
object 1, object2 and object3
Now object1 is referencing the clock object

Correct. What confuses some people is that the Clock object is still a
Clock object.
It is also an Object object because Clocks are Objects.
So the object1 references only the Objectness and not the Clockiness.
It sounds like you understand this correctly, so I am really speaking to
those who may be confused.
object2 is referecing the boxed int
Yup

object3 is referencing a string object.

Yup again, although you can currently only get at the Object behaviors.
You need to cast it to string if you wish to get at the Stringiness.
Can you just correct me if I'm wrong at some point in the above
description

Looks good to me.
Just as a note. I assume the example was just for discussion.
As a general rule it is a bad idea to mix different unrelated types in
an arraylist.

Bill
 
N

Nicholas Paldino [.NET/C# MVP]

See inline.

Bill Butler said:

That's not completely correct. It means that the internal storage of
the ArrayList is of type object, and can hold references to anything that
derives from object (which means everything). The types that are added to
the ArrayList are whatever type they are. If you add an int, it's still an
int (although boxed), a Button is still a button.
 
B

Bill Butler

Nicholas Paldino said:
See inline.



That's not completely correct. It means that the internal storage
of the ArrayList is of type object, and can hold references to
anything that derives from object (which means everything). The types
that are added to the ArrayList are whatever type they are. If you
add an int, it's still an int (although boxed), a Button is still a
button.

<grin>
I originally had verbiage very similar to what you wrote there.
I finally deleted it before sending and added additional info below.
Perhaps I shouldn't have deleted it



- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Correct. What confuses some people is that the Clock object is still
a Clock object.
It is also an Object object because Clocks are Objects.
So the object1 references only the Objectness and not the Clockiness.
It sounds like you understand this correctly, so I am really speaking
to those who may be confused.


Yup again, although you can currently only get at the Object
behaviors.
You need to cast it to string if you wish to get at the Stringiness.


Looks good to me.
Just as a note. I assume the example was just for discussion.
As a general rule it is a bad idea to mix different unrelated types
in an arraylist.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top