Java Vector

M

Marmagya

Hello,

I am trying to find out if I can populate a Vector in java with more
than data types:

-----------
import java.util.*;
import java.lang.*;

public class DiffVector {
class A {
String a = null;
A(){
a = new String("java");
}
}

void createVector() {
Vector v = new Vector();

v.addElement(new Integer(1));

DiffVector var = new DiffVector();
DiffVector.A a = new DiffVector.A();
v.addElement(a);

System.out.println("the vector is : " + v);

return;
}
public static void main(String args[]) {
DiffVector var1 = new DiffVector();
var1.createVector();
return;
}
}
-----------

The output of this is : "the vector is : [1, DiffVector$A@bd0108]"

I am expecting that either this program should not compile since I am
inserting data of different types, or it should display the second
element as "java".

Can someone tell me why I am getting this output?

Regards
Vineet
 
M

Marmagya

Hi Mike,

Thanks, this worked.

Regards
Vineet

Hello,

I am trying to find out if I can populate a Vector in java with more
than data types:

-----------
import java.util.*;
import java.lang.*;

public class DiffVector {
class A {
String a = null;
A(){
a = new String("java");
}
}

void createVector() {
Vector v = new Vector();

v.addElement(new Integer(1));

DiffVector var = new DiffVector();
DiffVector.A a = new DiffVector.A();
v.addElement(a);

System.out.println("the vector is : " + v);

return;
}
public static void main(String args[]) {
DiffVector var1 = new DiffVector();
var1.createVector();
return;
}
}
-----------

The output of this is : "the vector is : [1, DiffVector$A@bd0108]"

I am expecting that either this program should not compile since I am
inserting data of different types, or it should display the second
element as "java".

Can someone tell me why I am getting this output?


You add to elements to your vector, the 1st is Integer(1), this is output.
The 2nd is an Object of class DiffVector.A this class has no toString method
so Object.toString is called which is specified to produce a string that
represents class name and hash code; this is what is output.
You must implement toString to do what you want it to do.
 
V

VisionSet

Marmagya said:
Hello,

I am trying to find out if I can populate a Vector in java with more
than data types:

-----------
import java.util.*;
import java.lang.*;

public class DiffVector {
class A {
String a = null;
A(){
a = new String("java");
}
}

void createVector() {
Vector v = new Vector();

v.addElement(new Integer(1));

DiffVector var = new DiffVector();
DiffVector.A a = new DiffVector.A();
v.addElement(a);

System.out.println("the vector is : " + v);

return;
}
public static void main(String args[]) {
DiffVector var1 = new DiffVector();
var1.createVector();
return;
}
}
-----------

The output of this is : "the vector is : [1, DiffVector$A@bd0108]"

I am expecting that either this program should not compile since I am
inserting data of different types, or it should display the second
element as "java".

Can someone tell me why I am getting this output?

You add to elements to your vector, the 1st is Integer(1), this is output.
The 2nd is an Object of class DiffVector.A this class has no toString method
so Object.toString is called which is specified to produce a string that
represents class name and hash code; this is what is output.
You must implement toString to do what you want it to do.
 
C

Chris Smith

Marmagya said:
Vector v = new Vector();

v.addElement(new Integer(1));

DiffVector var = new DiffVector();
DiffVector.A a = new DiffVector.A();
v.addElement(a);

System.out.println("the vector is : " + v);

Incidentally, the line "DiffVector var = new DiffVector();" doesn't do
anything above. It creates a useless object, which is then ignored.
Some more pedantic compilers (Eclipse with certain warnings enabled, for
example) could warn you that you aren't using that variable anywhere.

If you expected that line of code to do anything, then you should
rethink your understanding of what's happening there.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

System.out.println("the vector is : " + v);

your basic problem is Vector does not have a decent toString method.
It just dumps the address of the vector.

You must interate over the Vector, fish out each element, cast it
appropriately, and print each element appropriately.
 
R

Raymond DeCampo

Roedy said:
your basic problem is Vector does not have a decent toString method.
It just dumps the address of the vector.

You must interate over the Vector, fish out each element, cast it
appropriately, and print each element appropriately.

Or do this:

System.out.println("the vestor is : " + new ArrayList(v));

Since ArrayList has an acceptable toString() method.

Also handy is:

Object arr[] = makeArray();
System.out.println("the array is : " + Arrays.asList(arr));

Although I would only recommend this in debug statements that will not
be invoked in production code..

HTH,
Ray
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top