Solved

Question about using JAVA Vector()

Posted on 2004-10-26
189 Views
Last Modified: 2010-03-31
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?
0
Question by:chenwei
    6 Comments
     
    LVL 37

    Accepted Solution

    by:
    Try casting

        ((SecondClass)v.elementAt(i)).print("test");
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>v.elementAt(i).print("test");  // I hae problem here


    should that be

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

    ?
    0
     
    LVL 6

    Expert Comment

    by:expertmb
    v.elementAt(i).print("test");  // I hae problem here
    should be
    ((SecondClass) v.elementAt(i)).print("test");  // I hae problem here
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    and it should be j indeed
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 50
    Jasper Report: Crosstab Report- Include Page Footer 2 22
    scores100 challenge 3 49
    commonTwo  challenge 63 45
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now