Home > Archive > Java Help > November 2005 > About cast
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Jordi 2005-11-07, 10:03 pm |
| Hi.
I've created a class called TokenizedFile as follows (simplified version
;-))
public class TokenizedFile
{
private String FileName;
TokenizedFile(String FileName)
{
this.FileName = FileName;
... some other things ...
}
public String getFileName()
{
return this.FileName;
}
}
Then, in the main code, I have an ArrayList of TokenizedFiles as follows:
ArrayList aL = new ArrayList();
TokenizedFile tf1, tf2, ... tfN;
aL.add(tf1);
aL.add(tf2);
....
aL.add(tfN);
Now, I would like to be able to print all the filenames. So, I tried to
do the next:
System.out.println(aL.get(0).getFileName());
But it does not work :-( I tried to do some cast as follows:
System.out.println(aL.get(0).(TokenizedFile)getFileName());
or
System.out.println(aL.get(0)(TokenizedFile).getFileName());
but they do not work. Does somebody know which is the right way to use
cast there?
Thank you.
Jordi.
| |
| klynn47@comcast.net 2005-11-07, 10:03 pm |
| Try this.
System.out.println(((TokenizedFile)aL.get(0)).getFileName());
| |
| Jordi 2005-11-07, 10:03 pm |
| It worked perfectly. Thank you very much ;-)
Jordi.
klynn47@comcast.net wrote:
> Try this.
>
> System.out.println(((TokenizedFile)aL.get(0)).getFileName());
>
| |
| Bjorn Abelli 2005-11-07, 10:03 pm |
| > klynn47@comcast.net wrote:
[color=darkred]
If all the OP wants is to print the "FileNames", he could also just add the
method toString() to his class:
public class TokenizedFile
{
private String FileName;
TokenizedFile(String FileName)
{
this.FileName = FileName;
... some other things ...
}
public String getFileName()
{
return this.FileName;
}
public String toString()
{
return this.FileName;
}
}
....and then he could print it like this:
System.out.println( aL.get(0) );
;-)
// Bjorn A
| |
| Roedy Green 2005-11-07, 10:03 pm |
| On Sat, 05 Nov 2005 19:32:32 +0100, Jordi
<despertaferro@gmail.NOSPAM.com> wrote, quoted or indirectly quoted
someone who said :
>Then, in the main code, I have an ArrayList of TokenizedFiles as follows:
>
>ArrayList aL = new ArrayList();
>TokenizedFile tf1, tf2, ... tfN;
>
>aL.add(tf1);
>aL.add(tf2);
>...
>aL.add(tfN);
>
>Now, I would like to be able to print all the filenames. So, I tried to
>do the next:
If by "main code" you mean the static main method, your tokenized file
name is not available there.
I presume what you mean here is not that you wrote this as Java code,
but that you wished for a feature to get you the effect of adding
multiple files or TokenizedFiles
You might simply add each filename you get in the constructor into a:
static ArrayList<Tokenized> allFiles = new
ArrayList<TokenizedFile>(20);
with:
allFiles. add( this );
Then you don't need any sort of 1 2 3 .. numbering.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
|
|
|