How to get 2d array from ArrayList of arrays in java -
i need dynamically create string[][] variable in java. tried code got exception java.lang.classcastexception: [ljava.lang.object; cannot cast [[ljava.lang.string; why , how can create such array?
string [][] gettitles() { string [][] res= {{""}}; arraylist <string []> titles=new arraylist(); resourcebundle rb=(resourcebundle)config.get(session, "messages"); string titlestring=null; int i=0; try { rb.getstring(getresourcenameprefix()+"-titles-"+i); } catch(exception e) { titlestring=null; } while(titlestring!=null) { string [] title=titlestring.split("\\|",-1); if(title.length==1 && title[0].length()==0) { title=new string [0]; } titles.add(title); i++; try { rb.getstring(getresourcenameprefix()+"-titles-"+i); } catch(exception e) { titlestring=null; } } res=(string [][])titles.toarray(); return(res); }
you can use method toarray(t[] a)
string[][] twodarray = titles.toarray(new string[titles.size()][]);
Comments
Post a Comment