Results 1 to 3 of 3
Thread: Pblm during typecasting
- 12-31-2009, 08:19 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0
Pblm during typecasting
How to do type casting in java for Object[][] class.
I have one code.
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
while(((line = br.readLine()) != null) && flag != true )
{
String[] theline = line.split("\\s");
for (int i = 0; i < theline.length; i++)
{
///////////////////////////////////////Part1 /////////////////////////////////////////
ArrayList s2 = new ArrayList();
s2.add(Integer.toString(i+1));
s2.add(theline[i]);
s2.add("-----");
s2.add(new Boolean(false));
rows.add(s2);
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Part2 /////////////////////////////////////////////
String[] s1 = new String[4];
s1[0] = Integer.toString(i+1);
s1[1] = theline[i];
s1[2] = "------";
s1[3] = "new Boolean(false)";
rows.add(s1);
///////////////////////////////////////////////////////////////////////////////////////////////////////
}
flag = true;
}
data = new Object[rows.size()][];
data = (Object[][])rows.toArray(data); // working fine for part2 code -- (45)
}
I am reading the data from the file and storing the data into ArrayList.When I do the type casting there is error (line 45). While when part two (string array ) is used it is working fine (line 45). May I know where is the pblm. How can use ArrayList instead of String array for storing the data to match with class Object[][]. I am in great need. Please help!
Thanks in advance.
- 12-31-2009, 08:29 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you have an ArrayList where an array is expected then you can convert it to an array using the toArray method. So when you add the arraylist in part one, add it's toArray instead.
- 12-31-2009, 08:55 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 14
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks