Results 1 to 20 of 20
Thread: convert string to list
- 06-15-2010, 03:31 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
- 06-15-2010, 04:04 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
String can be use as an element of a list. What you really means by converting to a list?
- 06-15-2010, 04:09 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
from this class it return as java.util.list
now need to convert it so that it return as well as java.util.list to here..but how?.
.
.
.
.
while(rs.next()){
List list = new ArrayList();
String x1 = rs.getString("x");
list.add(x1);
list1.add(list);
String obj = (String) list1.get(0);
- 06-15-2010, 04:16 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Well, you can't just convert a List to a String. You have added your List list to what appears to be another List list1. So you can't just pull a String off of list1 -- you have to pull a List off, and then you can pull a String out of that.
If you share more of your code, maybe we can help you find where you are getting confused.
-Gary-
- 06-15-2010, 04:28 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
this line is giving me java.lang.ClassCastExceptionString obj = (String) list1.get(0);
i do not know how to convert it
- 06-15-2010, 04:31 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
As I told you, you can't convert it. You didn't add() a String to list1, you added a List to it.
So you can't get() a String from there -- you can only get() a List -- and then that List has a String on it.Java Code:String x1 = rs.getString("x"); list.add(x1); [COLOR="Red"]list1.add(list); [/COLOR]
-Gary-
- 06-15-2010, 04:50 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
still having error here
String obj = (String) list1.get(0);
- 06-15-2010, 05:07 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
in other class :protected List....
.
.
List list1 = new ArrayList();
.
.
while(rs.next()){
List list = new ArrayList();
String x1 = rs.getString("x");
list.add(x1);
list1.add(list);
those read line giving me error on java.lang.ClassCastExceptionprotected void ....
if (list1 !=null && list1.size() >0)
{ String obj = (String) list1.get(0);
- 06-15-2010, 10:23 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Instead of using ArrayList and List, use the proper format ArrayList<String>, ArrayList<WhateverClassYouAreStoring>, that sort of thing.
That way you might actually find out what you are storing in the List.
You have been told several times now that you are trying to cast a List (which is what list1.get(0) is returning) into a String, and that is an illegal operation.
- 06-15-2010, 10:26 AM #10
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
so what exactly the correct way should i code?
- 06-15-2010, 10:37 AM #11
example
Java Code:import java.util.ArrayList; import java.util.List; public class ListExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("String 1"); list.add("String 2"); String s = list.get(0); System.out.println(s); } }
- 06-15-2010, 10:44 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
- 06-15-2010, 10:50 AM #13
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
yes thats true.
- 06-15-2010, 11:31 AM #14
that too in you code snippet you are creating the frest list for each and every traversal in ResultSet loop.
How will it maintain all the elements?Ramya:cool:
- 06-15-2010, 11:54 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
They're populating a List<List<String>>, though it's not declared as such.
So the List created in the loop through the resultset is the inner list, one for each result.
- 06-15-2010, 01:39 PM #16
Perhaps you could see what list1.get(0) is by using println() to show it before doing the assignment.String obj = (String) list1.get(0)
- 06-15-2010, 02:14 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
It's a List.
from earlier -
ETA: They're basically taking the resultset and turning each row in to a List<String>, and sticking them into a List<List<String>>. All because, I presume, they can't be bothered to create a proper object for the data.Java Code:List list1 = new ArrayList(); . . while(rs.next()){ List list = new ArrayList(); String x1 = rs.getString("x"); list.add(x1); list1.add(list); }
- 06-16-2010, 01:57 AM #18
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
so what should i change to return back same as the earlier code for this line?
String obj = (String) list1.get(0)
- 06-16-2010, 02:05 AM #19
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
That's a best guess based on the code you've shown us, but you've never shown us all the code, so I can't be sure if it's what you want. But more importantly, you really should review the concepts here, and investigate the generic forms of List and ArrayList.Java Code:. . . List newList = (List)(list1.get(0)); String obj = (String)(newList.get(0)); . . .
-Gary-
- 06-16-2010, 09:01 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
convert obj to string
By isme in forum New To JavaReplies: 11Last Post: 06-14-2010, 10:54 AM -
convert number to string
By wizard_oz in forum New To JavaReplies: 9Last Post: 11-16-2009, 07:04 PM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM -
convert InetAddress to a string
By hunterbdb in forum New To JavaReplies: 4Last Post: 10-18-2008, 09:50 AM -
How to convert List to Array
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks