Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quick question on vectors

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
this is a really simple question, but it escapes me at the moment, and I don't have a book with me.<br><br>Do all objects inside of a vector need to be of the same type?<br><br>Thanks.. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
I have just finished playing with vectors. To answer your question, technically yes. They have to be of type object :) <br><br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
*smile* thanks.. but you can have objects of different types, say an object of type java.awt.Button in the same vector as java.awt.TextArea ?<br><br>thanks again... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
From what I understand you can have different objects within the vector. In the info that I have read, that is one of the things that you have to watch for. The other thing that I have read is that ever object is of type object. for example:<br><br>if you have a class foo, then you extend duck from foo, then duck could also be considered an object of type foo.<br><br>public class foo<br>{<br>&nbsp;&nbsp;//stuff in here<br>}<br><br>public class duck extends foo<br>{<br>//stuff in here<br>}<br><br>What you could do with this is create an array of type foo<br><br>foo test1 = new foo();<br>duck test2 = new duck();<br><br>foo[] test = new foo[2]<br><br>foo[0] = test1;<br>foo[1] = test2;<br><br>This is similar to what happens with a vector, except the objects don't have to be related as directly as above (as far as I know). I should have been a little more clear on the last post, I hope this helps.<br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
well everything must be of type &quot;Object&quot; just because of polymorphism- since every class must inherit from object somehow, everything is an object, and so everything can be referred to by &quot;Object&quot;.<br><br>Basically, what I meant was this... you can't have an array that includes a mixture of ints and reals. But can you have a vector of ints and reals.<br><br>So that would mean that you can't have an array of foos and ducks... So I don't think that would work. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
I believe you can have a Vector of Foos and ducks.&nbsp;&nbsp;They are all objects.&nbsp;&nbsp;You must know how to cast them back to a foo and a duck so you can use them.
 
you can have any type of objects in a vector, or any mix in the same vector...you just need to cast them back correctly when you are reading them <p>Cal<br><a href=mailto: > </a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top