Question about using JAVA Vector()

My program looks as follow:

class FirstClass extends ...
{
...
...
 Vector v = new Vector();
 for(int i=0; i<5; i++)
 {
   v.add(i, new SecondClass());
  }
 ...
 ...
 for(int j=0; j<v.length; j++)
   v.elementAt(i).print("test");  // I hae problem here
}

class SecondClass extends JComponent
{
 ...
 ...
 public void print(String msg)
 {
   System.out.println(msg);
 }
}

But I get problem as noted above. Why?
chenweiAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Try casting

    ((SecondClass)v.elementAt(i)).print("test");
0
 
CEHJCommented:
>>v.elementAt(i).print("test");  // I hae problem here


should that be

v.elementAt(j).print("test");  // I hae problem here

?
0
 
expertmbCommented:
v.elementAt(i).print("test");  // I hae problem here
should be
((SecondClass) v.elementAt(i)).print("test");  // I hae problem here
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
elementAt returns an object of type Object.
The class Object doesn't have a print() function.
So you have to cast the Object to a SecondClass object which has the print() function
0
 
zzynxSoftware engineerCommented:
and it should be j indeed
0
 
zzynxSoftware engineerCommented:
Thanks
0
All Courses

From novice to tech pro — start learning today.