1

I'm currently working on a big app and I found a little Detail. It's possible to serialize Arrays and put them in a bundle. Then put it in a intent and start the activity. But on the receiving end I have to deserialize the array through a painful 2 step procedure.

     MyObj[] data = (MyObj[])bundle.getSerializable("key"); // doesn't work

     Object[] temp = (Object[])bundle.getSerializable("key");
     MyObj[] data2 = (MyObj[])temp, // doesn't work

     MyObj[] data3 = new MyObj[temp.length]; // does work
     for(int i = 0; i < temp.length; i++) {
          data3[i] = (MyObj)temp[i];
     }

What's the reason that I have to go through looping through the array?

Gray
  • 115,027
  • 24
  • 293
  • 354
schlingel
  • 8,560
  • 7
  • 34
  • 62
  • That is rather a general Java question than an Android specific one. Googling for "java casting arrays" will give answers to your problem. Java simply does not allow casting of Arrays downwards. – js- Dec 07 '11 at 16:52
  • Related to http://stackoverflow.com/questions/1115230/casting-object-array-to-integer-array-error – Gray Dec 07 '11 at 18:26

2 Answers2

6

The issue is that if you have an array of Object that you cast to be an array of MyObj, the compiler would have to go through and verify the class of each of the items in the array to allow the cast to be MyObj[]. The Java language designers made a decision to not do that and to force the programmer to write it out. For example:

Object[] objs = new Object[] { "wow", 1L };
// this isn't allowed because the compiler would have to test each object itself
String[] strings = (String[]) objs;
// you wouldn't want an array access to throw the ClassCastException
String foo = strings[1];

So the Java language forces you to do the loop yourself.

Object[] objs = new Object[] { "wow", 1L };
String[] strings = new String[objs.length];
for (int i = 0; i < objs.length; i++) {
    // this can then throw a ClassCastException on this particular object
    strings[i] = (String) objs[i];
}

You can use the Arrays class (which uses the System.arraycopy() native method) to easily do this:

MyObj[] data3 = Arrays.copyOf(temp, temp.length, MyObj[].class);

See: How to convert object array to string array in Java

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
0

You can also use JSON with which it is very very easy to serialize and deserialize an array AND you don't have any ugly casts in your code:

Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};

// Serialization
gson.toJson(ints);     ==> prints [1,2,3,4,5]
gson.toJson(strings);  ==> prints ["abc", "def", "ghi"]

// Deserialization
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); 

Example from: https://sites.google.com/site/gson/gson-user-guide#TOC-Array-Examples

Tobias
  • 7,282
  • 6
  • 63
  • 85
  • I use Gson already for the json/rest communication to the server and it's slower than java de/serializing. So I think I use the casting. – schlingel Dec 07 '11 at 17:13