Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Shawn - 15 Sep 2006 14:24 GMT
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;
    }
}
Patricia Shanahan - 15 Sep 2006 14:44 GMT
> Hi,
>
[quoted text clipped - 6 lines]
> (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
Shawn - 15 Sep 2006 15:06 GMT
>> (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)...

> Patricia

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.
Hendrik Maryns - 15 Sep 2006 15:19 GMT
Shawn schreef:

>>> (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)
[quoted text clipped - 6 lines]
> 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
Patricia Shanahan - 15 Sep 2006 16:36 GMT
>>> (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)
[quoted text clipped - 6 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.