This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Writeing / Reading ArrayList

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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):
>
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without the -Xlint option it comes up with warnings:

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic