Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
nick danasi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i tried that and was getting error cannot cast.can you give an example please.it would be helpful
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, but an example of what?
 
nick danasi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
copying linked list integer to linked list string
 
Paul Clapham
Sheriff
Posts: 20713
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m able to convert from integer to string using tostring but getting stuck with the next step??
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i could do.can anyone help?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
nick danasi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was trying to change from integer value to string using valueOf.am i wrong about it?
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the String like that, but you are not using it.
 
nick danasi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then please help me been trying for it since very long time.
 
nick danasi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10076
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nick danasi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic