• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Generics Problem - help with this code please  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - very new to java and I'm still struggling with generics - the syntax more than the concepts i think, but anyway ....

here is my sample code:



In the method addRow() line 15, I'm getting a compiler error where noted. I'm confused because daoArray should already BE an array of type "Row" from the constructor right?  Please advise and THANKS!

KenM
 
Saloon Keeper
Posts: 10136
214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I do the following:

That is what the compiler is protecting you against.

Maybe you intend your Row class to be generic as well, and then declare the list as:
 
Ranch Hand
Posts: 3218
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ken Matson wrote:I'm confused because daoArray should already BE an array of type "Row" from the constructor right?


Why is that?  Nothing in your code says it's an ArrayList<Row>, rather it's an ArrayList<E> - and E could be anything at this point.  What's the relationship between E and Row?  Should E be a type of Row?  Do you need a type E at all?  Maybe it's always an ArrayList<Row>, and there's no need for E at all.  It's hard to tell right now which way you want to go...
 
Marshal
Posts: 64172
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that you are trying to fill the List with the wrong sort of object:-
  • 1: Don't call an ArrayList xyzArray. It isn't an array.
  • 2: Only set the capacity of an array list if you expect to add that many elements to it. Setting the capacity to 20 doesn't mean it will accommodate 20 items. It will accommodate any number of items, but will accommodate 20 without increasing its capacity. Lists aren't like arrays, which have a fixed length.
  • 3: Why have you got that inner class? And why is it called Row? It doesn't appear to represent a row, but a person.
  • 4: Why have you not given the Row class a constructor? How are you going to instantiate ti with “real” values for its fields?
  •  
    Ken Matson
    Ranch Hand
    Posts: 34
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just have to laugh and shake my head ..... I remember now why I rarely post to forums with questions - because everyone is much more interested in criticizing, second guessing, and going off on tangents than they are in actually answering the question - gotta prove how smart they are and how dumb I am I suppose LOL. It's like - you ask how to put gas in your lawnmower, and everyone starts telling you you've got the wrong kind of yard, and do you really need a lawnmower, and why is your lawnmower red and not green, etc. etc. etc.

    In the responses - I got one that was almost helpful -  partially- and the rest?... well see the above.

    Here's the thing. the code SAMPLE I gave in the OP was just that - a SAMPLE. It is not the real code I'm working with which is much more involved, lengthy, etc. and involves multiple classes, sub-classes, etc. I simply (or so I thought) wrote a little sample code fragment to illustrate the SPECIFIC casting issue I was having. I was not looking for a code or design review.

    But, to answer a couple of the questions anyway:
    - The DAO would be a super class for specific DAO objects. It will need to process multiple different "Row" objects which themselves are sub-classed from a generic row class - that's why it's generic.
    - I use "Row" because I'm a database-centric guy. To me the DAO is simply a table-cursor and the instances of the object in the array represent "rows" of that table-cursor. That's how I think and it works just fine. This object will be populated from a ResultSet.
    - Inner Class? I dunno - just came out that way in the sample code. Not relevant to the issue, and I rarely/never use them in real life. Again S-A-M-P-L-E

    Here's the thing - no one actually answered the problem which was a casting issue. The "right" answer, which I discovered on my own (an IDE suggestion actually I missed before) was that I need to have:

    That simple. That's all I needed. My NEW question becomes, I thought one of the points of generics was to avoid the old style of casting like that? I would have expected something more like:

    If anyone has thoughts on THAT specifically, I'd love to hear them.

    Thanks -shields up ...

     
    Campbell Ritchie
    Marshal
    Posts: 64172
    215
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ken Matson wrote:. . . you ask how to put gas in your lawnmower, and everyone starts telling you you've got the wrong kind of yard, and do you really need a lawnmower, and why is your lawnmower red and not green, etc. etc. etc. . . .

    If you cut green grass with a red lawnmower you will end up with short grass. If you program something badly, like elevators on aeroplanes, you can kill 300 people. We see people who really intend to use w=code even worse than yours. That is why we worry about the whole of your code.

    You take your car to the garage and ask for a new tyre. They don't notice your brakes hardly work any more. That is why we worry about the whole of your code.

    Yes, the whole idea of generics is to avoid casting. Which means if you are casting anything you aren't using generics right.
     
    Bartender
    Posts: 2230
    94
    Eclipse IDE Google Web Toolkit Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ken Matson wrote:... It's like - you ask how to put gas in your lawnmower, and everyone starts telling you you've got the wrong kind of yard, and do you really need a lawnmower, and why is your lawnmower red and not green, etc. etc. etc....


    You've got 3 responses to your post, each with some good bit of information. I suggest reading them well. SVH has pointed out what the compiler is protecting you against and CR has pointed out all the important bits that will make your program better.
     
    Ken Matson
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Ken Matson wrote:. . . you ask how to put gas in your lawnmower, and everyone starts telling you you've got the wrong kind of yard, and do you really need a lawnmower, and why is your lawnmower red and not green, etc. etc. etc. . . .

    If you cut green grass with a red lawnmower you will end up with short grass. If you program something badly, like elevators on aeroplanes, you can kill 300 people. We see people who really intend to use w=code even worse than yours. That is why we worry about the whole of your code.

    You take your car to the garage and ask for a new tyre. They don't notice your brakes hardly work any more. That is why we worry about the whole of your code.

    Yes, the whole idea of generics is to avoid casting. Which means if you are casting anything you aren't using generics right.



    Well sorry - none of the SAMPLE was the "whole of my code" - only a fragment to illustrate issue. To use your tire example, if i take the tire off the car and take it to the shop to get a flat repaired, I don't expect them to start querying me about the rest of my car, etc. Oh well - agree to disagree.

    So on a serious note - and I hear you on the generics/casting BTW - then how do I do this:

    suppose A is a superclass and B, C, D, E  are separate subclasses of A
    I have another class G which has on it an ArrayList L which Is defined generically so that I can populate it with B,C,D,E depending on where it is called from.
    So if I want my addNewItem() method up in G, where the ArrayList is generic, how do I add a new B,C,D or E object to it w/o the casting I showed?

    TIA
     
    Mike Simmons
    Ranch Hand
    Posts: 3218
    19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well Ken, it looks to me like both Stephan and I were addressing issues directly related to your compilation error.  Campbell was raising a number of other issues not directly related, but intended to be helpful.  I think that both Stephan and I were trying to get you to see that you're trying to put a round peg in a square hole.  And we aren't sure if you really want both to be round, or both to be square.  But we're pointing out issues and asking questions related to that fundamental mismatch.  If that bothers you, well, that's unfortunate.  Maybe you can cast the round peg to (square), and that will make the compiler happy.  But I suspect you will find out at run time that the cast isn't valid after all.
     
    Mike Simmons
    Ranch Hand
    Posts: 3218
    19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ken Matson wrote:suppose A is a superclass and B, C, D, E  are separate subclasses of A
    I have another class G which has on it an ArrayList L which Is defined generically so that I can populate it with B,C,D,E depending on where it is called from.
    So if I want my addNewItem() method up in G, where the ArrayList is generic, how do I add a new B,C,D or E object to it w/o the casting I showed?


    If the ArrayList is generic in class G, it needs a type there, call it type T which (I think?) extends A, so you have G<T extends A>.  Then in class G you should not try to mention types B, C, D, E at all, and instead write an add(T) method that adds a T element to the ArrayList.

    Outside class G, you can create a new G<B>() and call its add(B) method.  Or create a G<C>() and call its add(C) method.
     
    Sheriff
    Posts: 21719
    102
    Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ken Matson wrote:Here's the thing - no one actually answered the problem which was a casting issue. The "right" answer, which I discovered on my own (an IDE suggestion actually I missed before) was that I need to have:

    That simple. That's all I needed.


    It's definitely not what you needed. Unless E is Row, that will cause a ClassCastException when you're going to start using daoArray. You think it contains elements of your generic type E, but because you try to trick the compiler it will all of a sudden contain something else. I'm saying "trying to trick" because you failed at that. If you compile this, your compiler (or IDE) will show you a warning that you have an unchecked cast. That's because, unlike a cast like (String) r, the JVM will not cause any ClassCastException at this moment - the exception will come later. For instance, now you have a Row in daoArray, the following will fail:
     
    That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!