aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B " Watch "Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B " New topic
Author

Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

This is found in Chapter 4:

6.k Write a method that takes an instance of any type, makes an ArrayList of that type, and then adds the original instance to the ArrayList.

Does anyone have any clue how to solve this?

This is what I came up with. I think it is not quite solving the problem though.




It seems like there would be a way to take the method parameter type, in this case Object, and some how cast this to any type passed to the method. After this is done the program would insert new generics for the arraylist based on this cast. So instead of ArrayList<Object> it would say ArrayList<whateverYouPassedIn>. I can't think of an algorithm for this though.

Please tell me how I am doing.

Respectfully,

Ted North
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ted North wrote: So instead of ArrayList<Object> it would say ArrayList<whateverYouPassedIn>. I can't think of an algorithm for this though.

that is what you are doing. Object != java.lang.Object in your example, no?
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Seetharaman Venkatasamy wrote:
Ted North wrote: So instead of ArrayList<Object> it would say ArrayList<whateverYouPassedIn>. I can't think of an algorithm for this though.

that is what you are doing. Object != java.lang.Object in your example, no?


Is this not a valid override of java.lang.Object? Would adding the 'public' modifier correct this?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ted North wrote:
Is this not a valid override of java.lang.Object? Would adding the 'public' modifier correct this?

Hmmm... Class A {} . could your Object class become a implicit super class of this?
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Seetharaman Venkatasamy wrote:
Ted North wrote:
Is this not a valid override of java.lang.Object? Would adding the 'public' modifier correct this?

Hmmm... Class A {} . could your Object class become a implicit super class of this?


I dont think my 'Object' class could implicitly be the super class of the class A {} you mentioned.

So what you are saying is that my new Object class is an implicit subclass of java.lang.Object?
I added in a few print statements that show that Object is a subclass of java.lang.Object... but still is this the way to solve this problem, strange class inheritance issues aside?

A colleague of mine was saying that reflection may be necessary to solve this...

Please find my changed code below, with the S.O.P. statements showing class heirarchy.



Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
sigh...

There are times I wanna shoot myself. Guys, I can't quite recall what I had in mind for that project but it certainly was NOT some sort of reflection thingy.

I think I had a much, much simpler idea in mind that was more along the lines of:

"You pick a type and then..."

as opposed to something that works for any arbitrary type.

Sorry!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1


lol and Thanks for the response Bert!

Could you please give a possible solution to the code?

I told some of my colleagues that you responded to my question, but they probably don't believe me.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
6.k Write a method that takes an instance of any type, makes an ArrayList of that type, and then adds the original instance to the ArrayList.


Seems to me the idea is to use generics. Reading the problem, my first thought was this:
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Dennis Deems wrote:
6.k Write a method that takes an instance of any type, makes an ArrayList of that type, and then adds the original instance to the ArrayList.


Seems to me the idea is to use generics. Reading the problem, my first thought was this:



Hi Dennis,

Thank you for the reply and sample code. I never would of thought of doing this. This is a program I created to test out your method and to my surprise it works like a charm.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange Collections Question from OCP Java SE 6 Programmer Practice Exams book by K & B