1

I have this collection

ArrayList<int[]>[] myList;

that is, an array of ArrayLists of integer arrays. I am trying to pass it in an intent.

intent.putExtra(getString(R.string.app_name) + ".myList", myList);

In the new activity I try to get the extra as:

ArrayList<int[]>[] myList;

Intent intent = getIntent();
myList = (ArrayList<int[]>[]) intent.getSerializableExtra(getString(R.string.app_name) + ".myList");

However I am getting a class cast exception in that line.

Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.util.ArrayList[]

As they contain arrays of primitive types and ArrayList is a serializable class itself, I am at a loss as to why this isn't working or what would be a better way to proceed.

EDIT

It appears that one can't just cast Object[] to CustomClass[]

Check:

Quick Java question: Casting an array of Objects into an array of my intended class

casting Object array to Integer array error

So one solution would be

Object[] tmp = (Object[]) intent.getSerializableExtra(getString(R.string.app_name) + ".myList");
myList = Arrays.copyOf(tmp, tmp.length, ArrayList[].class);

Or alternatively, one can pass and retrieve an ArrayList

// Calling Activity
ArrayList<ArrayList<int[]>> myIntentList = new ArrayList(Arrays.asList(myList));
intent.putExtra(getString(R.string.app_name) + ".myList", myIntentList);

// Called Activity
ArrayList<ArrayList<int[]>> myIntentList;

myIntentList = ArrayList<ArrayList<int[]>> intent.getSerializableExtra(getString(R.string.app_name) + ".myList");
Community
  • 1
  • 1
  • Do you have incontravertible evidence for this? Because the class cast exception seems to be saying that an `ArrayList[]` has been turned into an `Object[]` by Java serialization/deserialization, and that doesn't pass the evidentiary "smell test". – Stephen C Nov 02 '14 at 11:58

3 Answers3

0
intent.putIntegerArrayListExtra("int", int);

and

Bundle extras = getIntent().getExtras();
extras.getIntegerArrayList("int");
0

I would suggest to do

Serializable ser = (Serializable) list;
String key = getString(R.string.app_name) + ".myList";
intent.putExtra(key, ser);

in case the ArrayList[] is not recognized as Serializable.

hax
  • 26
  • 2
  • Hi! Sadly, casting the array to serializable still throws the same exception when I try to cast the return value of getSerializableExtra to (ArrayList[]). I know writing the array to an OutputObjectStream, passing its content in an extra as a ByteArray and then casting the return value of readObject of an InputObjectStream created from this ByteArray works. But I figured there must be a simpler or more proper way of doing this. – cthulhufhtagn Nov 02 '14 at 13:07
0

Setup an ArrayList<int[]>[]:

int[] values1 = new int[] { 1, 2, 3 ,4 };
ArrayList<int[]> al1 = new ArrayList<int[]>();
al1.add(values1);

int[] values2 = new int[] { 1, 2, 3 ,4 };
ArrayList<int[]> al2 = new ArrayList<int[]>();
al2.add(values2);

ArrayList<int[]>[] arrayOfIntArrays = new ArrayList[] { al1, al2 };

Intent i = new Intent(this, DemoActivity.class);
i.putExtra("blah", arrayOfIntArrays);

Now, on the receiving Activity, here is how you can reconstruct this:

Object[] objArray = (Object[]) getIntent().getExtras().getSerializable("blah");

List<?> objList = Arrays.asList(objArray);

ArrayList<int[]>[] arrayOfLists = new ArrayList[objList.size()];

arrayOfLists = objList.toArray(arrayOfLists);
// example value extraction
int[] values1 = arrayOfLists[0].get(0);

As to why you have to do all this? There are several somewhat advanced Java concepts at play here.

The first is that an int[] is not a primitive. Java treats any Array as an Object.

The second is that Generics in Java are subject to type erasure. This means that the typing of your ArrayList<int[]> doesn't really exist at runtime, it's essentially a kind of syntactic sugar.

You may also want to read about covariance and contravariance in Java Generics.

Obviously, there are probably cleaner ways to pass this data around in your application if you have control over the creation of these objects. For example, instead of an ArrayList<int[]>[] you could just use an ArrayList containing ArrayList<int>.

Jeffrey Mixon
  • 12,846
  • 4
  • 32
  • 55
  • Eventhough I had figured it out already as shown on my earlier edit, I'm accepting your answer for pointing to extra information. Thank you! – cthulhufhtagn Nov 02 '14 at 18:02