Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 808
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic