Monday, 15 March 2010

android - Convert 2D array to string and back again in Java? -


I think the easiest way is to convert a string into a SQLite database on Android with a 2d enum array

How can I do this in Java

  

MyEnum [] [] myArray.?

thanks

If you have a whole 2D-array one In the string, you can use a CSV-type encoding, but you want to preserve a particular character (type the compa-separator) so that the area does not interfere separation. A quick (and dirty?) Way to do this will be to use ENC = URLEncoder.encode (Val, "UTF-8") at each value, and then Back to Val = URLDecoder .decode (ENC, "UTF-8")

You also need to separate (like \ n ) another The separator will use lines.

  writing the string (MyENum [] [] myArray) {string rays = ""; (For int iCol = 0; iCol & lt; myArray [iRow] .length; iCol ++) race = = URLEncoder.encode (; IRow & LT; myArray.length; integer for iRow = 0 iRow ++) (MyArray [iRow] [iCol] .name (), "UTF-8") + ","; Res + = "\ n"; }}   

(I do not want to add additional "," at the end of each line). Then, to read back:

  MyEnum [] [] read (string race) {string [] rows = res.split ("\ n"); Myayemem [] [] myArray = New MyNum [Rows.Labi] []; (Int iRow; iRow & lt; rows.length; iRow ++) {string [] cols = rows [iRow] .split (","); Mayré [Irva] = New Myanm [cols.length]; MyArray [iRow] [iCol] = MyEnum.valueOf (URLDecoder.decode (Column [iCol], "UTF-8") for; (; ICol & lt; cols.length; integer iCol = 0 iCol ++); } MyArray to return; }   

All this is based on the fact that the name () and valueOf () are the methods that are available in your values , As @Sin-F had posted to you in the link.

No comments:

Post a Comment