Hi

I have stored File objects in a vector. Now I which to call the methods of the file objects stored in the Vector. Unlike C++ you cannot simply refer to the vector by index e.g.
vector.getAbsolutPath() nor can you simply call vector.elementAt(i).getAbsolutePath();

I have made a File[] array = new File[vector.size()]
then pass all the elements from the vector to the file array then parse to file and call the methods. This works but seems somewhat unprofessional and makes the code look untidy.

Also, the program may need to run again without closing making it an issue if the vector changes in size . The File[] array has a set size already making the program impossible to run a second time without having to close it first.

Is there a way I can access the File methods straight from the Vector?

Many thanks

If my sleepy mind does play tricks on me, you should do it as

(File)vector.elementAt(i).getAbsolutePath()

Many thanks for your attempt but even that does not work. The compiler objects to not knowing the method. I have tried more File methods like getName, length() and lastModified() all of which cannot be identified.

How about Simply calling the New File[vector.size()] to resize the array every time the vector size either increases or decreases? Is resizing an array in such a fashion bad practice or something that may be countenanced?

If my sleepy mind does play tricks on me, you should do it as

(File)vector.elementAt(i).getAbsolutePath()

I think, although it needs to be checked that you should do this: ((File)vector.elementAt(i)).getAbsolutePath() Just a suggestion, I don't know if it will work, just try it, because resizing the array is bad idea.
Every time you change the vector you will have to create a new array AND store again the data, so try first the above before using the array solution

commented: I totally agree :) +3

Yes it will work.

The following also works (and should actually be done this way, now):

Vector<File> vector = new Vector<File>();
// populate vector
String path = vector.elementAt(i).getAbsolutePath()
commented: Well coded :) +3
commented: The better one wins, ;) +11

Hi

I have stored File objects in a vector. Now I which to call the methods of the file objects stored in the Vector. Unlike C++ you cannot simply refer to the vector by index e.g.
vector.getAbsolutPath() nor can you simply call vector.elementAt(i).getAbsolutePath();

If you use Generics (see my last post), yes you can.

Many Thanks

It has worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.