Amazing: Vector can have elements of different types!!!

S

Shawn

Hi,

I just realized/found that in Java, Vector can have elements with
different types. I guess other collections are the same. See my code
below. I don't know Generics from Java 5.0. But I am really amazed that
heterogenous types of data can be put in the same place(Vector).

I have a couple questions:
(1)Is that common that people utilize this feature (putting different
types of data in one place)?
(2)Is there a way to find out what type of each element belongs to? (say
I forgot 1st element is Person, 2nd element is String, etc)

Thank you very much.

=============TestDemo.java============
import java.util.*;

public class TestDemo {
public static void main(String[] args)
{
Vector myVec = new Vector();
Person aPerson = new Person("Tom");
String aString = "Hello World";
int[] intArray = {3, 5, 9};

myVec.addElement(aPerson);
myVec.addElement(aString);
myVec.addElement(intArray);

Person newPerson = (Person)myVec.elementAt(0);
System.out.println("Here is the person:" + newPerson.getName());
//print Tom

String newString = (String)myVec.elementAt(1);
System.out.println("Here is the string:" + newString); //print Hello
World

int[] newArray = (int[])myVec.elementAt(2);
System.out.println("Here is the array element: " + newArray[1]);
//print 5
}
}

==============Person.java================
public class Person {
private String sName;

public Person()
{
//empty
}

public Person(String s)
{
this.sName = s;
}

public String getName()
{
return sName;
}
}
 
P

Patricia Shanahan

Shawn said:
Hi,

I just realized/found that in Java, Vector can have elements with
different types. I guess other collections are the same. See my code
below. I don't know Generics from Java 5.0. But I am really amazed that
heterogenous types of data can be put in the same place(Vector).

I have a couple questions:
(1)Is that common that people utilize this feature (putting different
types of data in one place)?

I don't usually mix classes whose only common superclass is Object. More
often, all the elements implement or extend a specific interface or
class other than Object.

The new generics features allow tagging of a Vector (or other java.util
collection) with the intended type of its contents.
(2)Is there a way to find out what type of each element belongs to? (say
I forgot 1st element is Person, 2nd element is String, etc)

if(a instanceof Person)...

Have you read a Java textbook? If not, I suggest doing so. You seem to
be discovering the basics of how to use Java by a mix of experimentation
and asking how to do things in this newsgroup, which is not very
efficient and may leave serious gaps in your Java skills.

Patricia
 
S

Shawn

Patricia said:
if(a instanceof Person)...

Sorry. I am from a procedure language world. Trying to look at Java from
a different angle, like passing a method as a parameter into another
method to build higher level method.

Could you elaborate how the following works:

if (a instance of Person) ...
if (a instance of String) ...

Thank you.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Shawn schreef:
Sorry. I am from a procedure language world. Trying to look at Java from
a different angle, like passing a method as a parameter into another
method to build higher level method.

Java does not do automatic type inference. The pre-1.5 collection
classes are not type aware. Generics is a (much debated) solution to
this, but only guarantees compile-type safety.
Could you elaborate how the following works:

if (a instance of Person) ...
if (a instance of String) ...

Read better: if (a instanceof String) { // a is a String
String bigA = ((String) a).toUppercase();
}

Alternatively: a.getClass(), or other reflection methods.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFCrZ2e+7xMGD3itQRAhE1AJ9Yzrjy3ZaC1n6S2/OH/5wEjqK1YACeO0gc
R/huH5gg63FE8dYOqRbgvNE=
=hUYo
-----END PGP SIGNATURE-----
 
P

Patricia Shanahan

Shawn said:
Sorry. I am from a procedure language world. Trying to look at Java from
a different angle, like passing a method as a parameter into another
method to build higher level method.

In my experience, it is very important to approach each language on its
own terms, and first learn the normal ways of doing things in that
language. For that, books are invaluable. Once you understand the normal
ways of doing things in Java, a lot of the features will seem much less
arbitrary.

Patricia
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top