Results 1 to 3 of 3
  1. #1
    man4ish is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

    Default 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  3. #3
    man4ish is offline Member
    Join Date
    Dec 2009
    Posts
    14
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •