File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
i am new to topic generics and i have a doubt and can anyone explain?
is it possible to copy linked list of integer values to a linked list string
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2835
    
  11

It would be possible to copy them, as long as converting the Integers to Strings was part of your copy process. You cannot simply cast LinkedList<Integer> to LinkedList<String> though.
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
yes i tried that and was getting error cannot cast.can you give an example please.it would be helpful
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2835
    
  11

Sure, but an example of what?
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
copying linked list integer to linked list string
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It should be easy for you to do that. Just write a loop which goes through the entries of the Integer list, converts each Integer to a String, and adds that String to the String list. Give it a try and let us know if you have any problems.
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
i m able to convert from integer to string using tostring but getting stuck with the next step??
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

Do you know how to loop through a list? Do you know how to add an element to a list?

Then you should be able to convert an integer list to string list
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
this is what i could do.can anyone help?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Look at this line in your code (which is inside the loop):

What do you think it does? What are you putting into the method?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
i was trying to change from integer value to string using valueOf.am i wrong about it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
You can get the String like that, but you are not using it.
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
then please help me been trying for it since very long time.
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
nick danasi wrote:then please help me been trying for it since very long time.

LinkedList<Integer> l1= new LinkedList<Integer>();
l1.add(3);//two other elements
String[] s= new String[];
int i=0;
s[i]=String.valueof(l1);
LinkedList<String> l2= new LinkedList<String>(Arrays.asList(s[i]));
System.out.print(l2);

is this code correct???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

No.

Note that l1 is the entire list. You can't pass the entire list to String.valueOf(...) at once. Get rid of the call String.valueOf(l1).

The first thing you need to do, is loop over the list l1 properly with a for loop.

Inside the loop, call String.valueOf(...) on i, the current element, and not on the entire list l1. Add the result of the call to the list l2.

Get rid of the string array. Don't randomly add things to your program that you don't understand - that way you're only getting into a bigger mess of things you don't understand. Carefully reason about what your program has to do and make sure you understand exactly what each line of code that you write does.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

nick danasi wrote:then please help me been trying for it since very long time.

Please EaseUp (←click). We're all volunteers here.

And further to Jesper's excellent advice, I'd add: use proper names.

You're not doing yourself - or anybody that has to read your code - any favours by using names like 'l1'. How about 'integerList', or simply 'integers'?

Anybody (well, almost anybody) can write programs that work. Good programmers write programs that other people can understand. Remember the maxim:
Michael Golding wrote:Always code as if the guy who ends up maintaining your code is a violent psychopath who knows where you live.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
nick danasi
Greenhorn

Joined: Dec 30, 2012
Posts: 15
thanks.i have another doubt a generics in unbounded wildcards.an unbounded wildcard for the Process Method and hash set to add String and Integer values to the objects using unbounded wildcards.wat does this mean unable to understand???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics
 
Similar Threads
Generics
Generics
Generics
Generics
generics