1

Currently I have the below code, where I am trying to cast from Object to String and I get an exception error.

What is the best way to convert from this Object array to String array so that it will work in my for loop? Or is there a way to alter my for loop to display the Object array?

<%

          Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:/Users/rhysparker/Documents/workspace/Resource_Planner/WebContent/db/RESOURCE.SQLITE");
                Statement stat = conn.createStatement();

                ArrayList<ArrayList<String>> al = new ArrayList<ArrayList<String>>();

                ResultSet rs = stat.executeQuery("select project_name from project;");

                ResultSetMetaData metaData = rs.getMetaData();
                int columns = metaData.getColumnCount();

                while (rs.next()) {
                    ArrayList<String> record = new ArrayList<String>();
                    for (int i = 1; i <= columns; i++) {
                        String value = rs.getString(i);
                        record.add(value);
                    }
                    al.add(record);
                }

                String[] testing = (String[])al.toArray();

                for(int i=0;i<testing.length;i++) 
                { %> 
                <option value="<%=testing[i]%>"><%=testing[i]%></option><% 
                } 

                rs.close();
               conn.close();
            %>
2
  • what exception it is throwing? Commented Feb 14, 2013 at 6:12
  • java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; Commented Feb 14, 2013 at 6:14

4 Answers 4

2
 I get an exception error.

Yes because al is type of ArrayList<String> ArrayList<ArrayList<String>> al = new ArrayList<ArrayList<String>>();

And your are casting it to a array of string

String[] testing = (String[])al.toArray();

so it will throw exception at run time like .

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

If you are adding Arraylist inside arraylist then its your responsibility to insure type checking.

You can do something like following :

for (ArrayList<String> list: al)
 {
    String[] testing = list.toArray(new String[0]); // or (String[]) arr.toArray();
    for(int i=0;i<testing.length;i++) 
     { %> 
        <option value="<%=testing[i]%>"><%=testing[i]%></option><% 
     }
 }
1
  • That is just what I am looking for. I will study up on array lists to get a better understanding. Thanks for your help Sumit. Commented Feb 14, 2013 at 6:26
0
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
String[] array = (String[])list.toArray(new String[list.size()]);

You mean something like this?Try this.

1
  • That throws the same exception. java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; Commented Feb 14, 2013 at 6:11
0

This is because your al is multidimensional (ArrayList of ArrayList -- containing a database table -- I suppose)
Below code will help you in filling your testing array

int maxColumn = 100;    //Assuming not more than 100 columns
int rowSize = a1.size();
String[] s1 = new String[maxColumn];
String[][] testing = new String[rowSize][maxColumn];    
for(int i=0; i<rowSize; i++)
{
    ArrayList a1 = al.get(i);
    s1 = a1.toArray();
    for(int j=0; j<s1.length; j++)
        testing[i][j] = s1[j];
}

testing is the 2-dimensional array which has the table record. Now to which column you need to assign in your <option> field that matters
Say you need to assign column 3
So your for-loop will become

for(int i=0;i<rowSize;i++) 
{ %> 
    <option value="<%= testing[i][3] %>"><%=testing[i]%></option>
<% } 
0

You are getting an exception because the elements inside the arraylist al that you are converting is of type ArrayList<String> which, ofcourse, cannot be cast to String[].

You must use 2 dimensional array for your code to work because your list al is of type ArrayList<ArrayList<String>> which means the elements inside your list is another list that contains String

To solve your problem, you must manually loop through your list then convert each element (which is ArrayList<String>) into String[]

int yLen = al.size();
        int xLen = al.get(0).size();
        String[][] testing = new String[yLen][xLen];
        for(int a = 0; a < yLen; a++){
            testing[a] = al.get(a).toArray(testing[a]);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.