473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Run Method on elements in a Vector

Jezternz
145 100+
Ok, basicly I have created a Class.
Expand|Select|Wrap|Line Numbers
  1. public class ClassX{
  2.     public ShapeObj(){}
  3.     public doThis(){}
  4. }
  5.  
And I have created a Vector and filled it with instances of this new class.
Expand|Select|Wrap|Line Numbers
  1. Vector xv = new Vector();
  2. xv.add(new ClassX());
  3. xv.add(new ClassX());
  4. xv.add(new ClassX());
  5. xv.add(new ClassX());
  6.  
Now I want to traverse through the Vector (preferably efficiently) and run the method (doThis) on each element. This is what I have:
Expand|Select|Wrap|Line Numbers
  1. ListIterator it = objlist.listIterator();
  2. while(it.hasNext()){
  3.     ClassX temp = it.next();
  4.     temp. doThis();
  5. }
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. ListIterator it = objlist.listIterator();
  2. while(it.hasNext()){
  3.     Object temp = it.next();
  4.     temp. doThis();
  5. }
  6.  
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
Aug 31 '09 #1
5 1890
JosAH
11,448 Expert 8TB
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
Aug 31 '09 #2
Jezternz
145 100+
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:
Expand|Select|Wrap|Line Numbers
  1. ListIterator it = objlist.listIterator();
  2. while(it.hasNext()){
  3.     ClassX temp = (ClassX)it.next();
  4.     temp. doThis();
  5. }
  6.  
Aug 31 '09 #3
JosAH
11,448 Expert 8TB
@Jezternz
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
Aug 31 '09 #4
Jezternz
145 100+
Thanks heaps, here are the two lines that need to be changed for generics use.
Expand|Select|Wrap|Line Numbers
  1. Vector<ClassX> xv = new Vector();
  2.  
and
Expand|Select|Wrap|Line Numbers
  1. ListIterator<ClassX> it = objlist.listIterator();
  2. while(it.hasNext()){
  3.     ClassX temp = it.next();
  4.     temp. doThis();
  5. }
  6.  
Aug 31 '09 #5
JosAH
11,448 Expert 8TB
@Jezternz
Almost, make that:

Expand|Select|Wrap|Line Numbers
  1. Vector<ClassX> xv = new Vector<ClassX>();
  2.  
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
Sep 1 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.