Run Method on elements in a Vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jezternz
    New Member
    • Jan 2008
    • 145

    Run Method on elements in a Vector

    Ok, basicly I have created a Class.
    Code:
    public class ClassX{
    	public ShapeObj(){}
    	public doThis(){}
    }
    And I have created a Vector and filled it with instances of this new class.
    Code:
    Vector xv = new Vector();
    xv.add(new ClassX());
    xv.add(new ClassX());
    xv.add(new ClassX());
    xv.add(new ClassX());
    Now I want to traverse through the Vector (preferably efficiently) and run the method (doThis) on each element. This is what I have:
    Code:
    ListIterator it = objlist.listIterator();
    while(it.hasNext()){
    	[B][U][I]ClassX temp = it.next();[/I][/U][/B]
    	temp. doThis();
    }
    The problem is, the compiler is complaining that it can only return an 'object' from the iterator. So naturally I tried changing it to this:
    Code:
    ListIterator it = objlist.listIterator();
    while(it.hasNext()){
    	Object temp = it.next();
    	[B][U][I]temp. doThis();[/I][/U][/B]
    }
    This was wishfull thinking though, as now the next line is the problem, as obviously the Object Class doesnt have a doThis() Method.
    So how can I cycle through the vector and run a method on each element?

    Cheers, Josh
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Use generics: you don't just have a raw Vector but a Vector<ClassX> that contains only classX objects. It gives you a dedicated iterator: Iterator<ClassX > that returns a ClassX object each time you ask for it.

    kind regards,

    Jos

    Comment

    • Jezternz
      New Member
      • Jan 2008
      • 145

      #3
      What do you mean by generics?
      Anyway I managed to get in contact with an old tutor, and he told me all I had to change was below:
      Code:
      ListIterator it = objlist.listIterator();
      while(it.hasNext()){
          ClassX temp = [B][I][U](ClassX)[/U][/I][/B]it.next();
          temp. doThis();
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Jezternz
        What do you mean by generics?
        Anyway I managed to get in contact with an old tutor, and he told me all I had to change was below:
        Code:
        ListIterator it = objlist.listIterator();
        while(it.hasNext()){
            ClassX temp = [B][I][U](ClassX)[/U][/I][/B]it.next();
            temp. doThis();
        }
        Yes, that's the old fashioned way of doing it: cast the Object to the type you want. Generics do the same but they do it compile safe. Read the tutorials; it was introduced in Java 1.5 and you should use it.

        kind regards,

        Jos

        Comment

        • Jezternz
          New Member
          • Jan 2008
          • 145

          #5
          Thanks heaps, here are the two lines that need to be changed for generics use.
          Code:
          Vector<ClassX> xv = new Vector();
          and
          Code:
          ListIterator<ClassX> it = objlist.listIterator();
          while(it.hasNext()){
              ClassX temp = it.next();
              temp. doThis();
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Jezternz
            Thanks heaps, here are the two lines that need to be changed for generics use.
            Code:
            Vector<ClassX> xv = new Vector();
            and
            Code:
            ListIterator<ClassX> it = objlist.listIterator();
            while(it.hasNext()){
                ClassX temp = it.next();
                temp. doThis();
            }
            Almost, make that:

            Code:
            Vector<ClassX> xv = new Vector<ClassX>();
            It tells the compiler that xv is a reference to a vector of ClassX elements. But yes, generics are better than explicit casts all over your code. Generics are a compile time issue, i.e. during runtime those types are checked by the compiler and erased.(i.e. they have become ordinary Object types again).

            kind regards,

            Jos

            Comment

            Working...