This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Writeing / Reading ArrayList  RSS feed

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Python
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Python
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks.
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!