Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

using parameterized ArrayList

Thread view: 
ebby83@gmail.com - 16 Mar 2006 03:40 GMT
I am trying to read write <Event> objects to an ArrayList ... but when
I try to convert the arraylist back to the object arrayit gives me  a
ClassCastException for the line

return (Event[]) a.toArray();

--------------------------------------
BufferedReader ois = null;
        ArrayList<Event> a = new ArrayList<Event>();
        try {

            ois = new BufferedReader(new FileReader(this.calFile));

            Event e = null;
            String line;

            while ((line = ois.readLine()) != null) {
                e = new Event(line);
                a.add(e);
            }
            ois.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (ParseException e) {

            e.printStackTrace();
        } catch (InvalidTimeRangeException e) {

            e.printStackTrace();
        }
        if (a.isEmpty())
            return null;
        return (Event[]) a.toArray();
Marcin Wielgus - 16 Mar 2006 03:50 GMT
> return (Event[]) a.toArray();

try

return (Event[]) a.toArray(new Event[0])

for more info read documentation of toArray(T[])

Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

roy2000@gmail.com - 16 Mar 2006 08:41 GMT
> > return (Event[]) a.toArray();
> >
> try
>
> return (Event[]) a.toArray(new Event[0])

or simply
return a.toArray(new Event[0])
Piotr Kobzda - 16 Mar 2006 09:55 GMT
>>>return (Event[]) a.toArray();
>>
[quoted text clipped - 4 lines]
> or simply
> return a.toArray(new Event[0])

or better:
return a.toArray(new Event[a.size()]);

piotr


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.