We help IT Professionals succeed at work.

How do I implement this interface method from Collection<E>     public <T> T[] toArray( T[] a) ?

scooby6939
scooby6939 asked
on
337 Views
Last Modified: 2008-03-03
   I am implementing the Collection<E> interface:  public <T> T[] toArray( T[] a). I need to place elements from a LinkedList<T> into it.  I cant get this method to work correctly.  Someone out there must have implemented this allready. Could someone let me know how you did it?

note: I don't think I can change T to Object. This is part of an assignment and I think he wants us to use generics for this.


Thank you

Scooby6939
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
If a (input parameter) is null then create an array of type T the same size as your list
Then iterate thru your list and add each element (of type T) to your array
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Are you truing to make your own collection class? Why? There are 100s already available.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
how is nhagiaubungbu's comment difrferent from mine?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
:-) its not.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
It's not. (S)he just posted some sample code and thus I thought (s)he also deserves some points.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
a single line would not seem like sample code.
and does that mean when someone suggests a method we can post a line using that method and we shouldn't get flamed. Cause that aint the case in my experience :)

anyways I'm happy with whatever u decide.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
I do not decide. I just recommend and sometimes it is difficult to recommend an exact possible solution.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Well, the sample is not really big enough - you can get most of it from the documentation link already provided. Maybe do a 400-100 split.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> I do not decide. I just recommend and sometimes it is difficult to recommend an exact possible solution.

I'm aware of that, just giving my view :)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Thanks for the clean-up work, girionis :)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes, you're doing a great job :)
My comment is different to objects :) He use toArray(T[]), and I recommend to use toArray(), easier to use. And I think, it's easier for some new Java-er if there are some example line.
Anyway, I thought the author DID forget this question. Watever ....
P.S: I'm HE, not SHE :)
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Thank you guys, I appreciate your support :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.