aspose file tools*
The moose likes Beginning Java and the fly likes Writeing / Reading ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Writeing / Reading ArrayList" Watch "Writeing / Reading ArrayList" New topic
Author

Writeing / Reading ArrayList

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, the next assignment we received was to take our prior program and convert it from an Array to an ArrayList. For the most part this worked well, but when I tried adding file code (read/write the ArrayList) I ran into problems. The write worked well, by well I mean that it didn't throw an error and it seemed to write the file. But, when I add the code to read the ArrayList the compiler doesn't seem to like reading in an ArrayList as an object. How is this done?

Note: the original assignment was just to write each element out to the file, I am trying to go beyond that and write the ArrayList out as a single object.


Error received:


Here is the code (problem is in the loadApplications method):
>
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Mike Lipay wrote:Ok, the next assignment we received was to take our prior program and convert it from an Array to an ArrayList. For the most part this worked well, but when I tried adding file code (read/write the ArrayList) I ran into problems. The write worked well, by well I mean that it didn't throw an error and it seemed to write the file. But, when I add the code to read the ArrayList the compiler doesn't seem to like reading in an ArrayList as an object. How is this done?

Note: the original assignment was just to write each element out to the file, I am trying to go beyond that and write the ArrayList out as a single object.


Error received:



That error tells you that the required type is java.util.ArrayList - which makes sens because the line is assigning a value to apps, and apps is an ArrayList. The error also tells you that the type returned from the ObjectInputStream#readObject() method is an java.lang.Object. Since the return type isn't an ArrayList you have a problem - the underlying runtime object might actually be an ArrayList but the compiler can't be sure.

Now, in your previous post you solved the same problem on the same line of code using an array instead of an ArrayList. How did you do that?


Steve
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Steve Luke wrote:That error tells you that the required type is java.util.ArrayList - which makes sens because the line is assigning a value to apps, and apps is an ArrayList. The error also tells you that the type returned from the ObjectInputStream#readObject() method is an java.lang.Object. Since the return type isn't an ArrayList you have a problem - the underlying runtime object might actually be an ArrayList but the compiler can't be sure.

Now, in your previous post you solved the same problem on the same line of code using an array instead of an ArrayList. How did you do that?



The Array version is:



I tried duplicating this code, but nothing I tried seemed to be allowed, for example:


This generated the error:


I'm not sure what the correct coding is to cast it properly, I can't find anything in javadoc that covers this, and we haven't covered it in class.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

That is a warning only, the class compiles and will run properly, assuming the content of the file actually is an ArrayList<Application>. The warning is telling you that you are 'down-casting' the object from a broader type to a more specific type, and that the compile has no way to know if it will work or not. There are ways to temporarily turn the warning off (such as not using -Xlint on the compile command) but you don't have to worry about them.

Question, when you compiled the Array version, didn't you get the same warning?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Steve Luke wrote:Question, when you compiled the Array version, didn't you get the same warning?


I guess not - looks like it only comes up relative to Generics.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Without the -Xlint option it comes up with warnings:

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I tried a compile and execute, it does work. Is there a better way to accomplish this so that the compile doesn't throw an error?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It isn't an error, it is a warning. Sorry, I was wrong about the -Xlint option, I thought it warning was a standard warning about the cast, I didn't remember it was about the generics cast. You can add the @SupressWarning("unchecked") annotation to the line of code:

Or you can just live with the warning since 'you know what you are doing' and know that that line of code won't actually break. I don't know of a compiler command that will hide both the warning and the note.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writeing / Reading ArrayList