11

Kotlin provides Array.isArrayOf() for checking if an array is of a certain type.

It's used like this

if(object.isArrayOf<String>())

And defined like this

/**
 * Checks if array can contain element of type [T].
 */
@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")
public fun <reified T : Any> Array<*>.isArrayOf(): Boolean =
    T::class.java.isAssignableFrom(this::class.java.componentType)

But it's only for Array. I need to check ArrayList.

I thought to change the signature like so.

@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")
public fun <reified T : Any> ArrayList<*>.isArrayListOf(): Boolean =
    T::class.java.isAssignableFrom(this::class.java.componentType)

but class.java.componentType is specific to Array

How can I check what type of ArrayList I have?

I should clarify, I only care if its one of 3 types, so I don't need a completely open-ended way of checking.

3
  • Why are you checking using ArrayList? It's not the base implementation Commented Nov 29, 2018 at 19:15
  • I need to direct it into the appropriate Bundle method. bundle.putStringArrayList(), bundle.putIntegerArrayList(), ect Commented Nov 29, 2018 at 19:16
  • Lists in general rely on generics. Because of type erasure, you can't check the generic type it has. Commented Nov 29, 2018 at 19:17

3 Answers 3

8

If you want to check the type of a list you can do:

when (list.firstOrNull()) {
   is String -> { /*do something*/ }
   is Int -> { /*do another thing*/ }
   else -> { /*do something else*/ }
}

And if you need to use the list of a certain type you can use:

list.filterInstance</*the type you need*/>()

Hope this works for you.

Sign up to request clarification or add additional context in comments.

Comments

4

You can't. Arrays are the only generic type for which this is possible (because they aren't really generic in the same sense, Kotlin just hides it).

The only thing you can do is look at its contents, but of course

  1. that won't work for empty lists;

  2. if a list contains e.g. a String, it could be ArrayList<String>, ArrayList<CharSequence>, ArrayList<Any>, etc.

For this purpose:

I need to direct it into the appropriate Bundle method. bundle.putStringArrayList(), bundle.putIntegerArrayList(), ect

neither should be a problem, I believe.

6 Comments

Explain how that isn't a problem? I have an object and I don't know if its an ArrayList of Parcelable or Integers. I can't just pass one into the other. I know they all just end up in a HashMap<String, Object> anyway, but I still need to call the right method.
Also, if it's an ArrayList of something that you can't put in a Bundle, then it would fail.
As I said, check the contents. If the first element is e.g. an Integer, cast to ArrayList<Int> etc. If the list is empty, then it doesn't matter what you cast it to, as all put* methods will just store 0 length.
Actually, after checking, since Bundle.put* methods don't look at the contents at all and just put the argument into a map, you can ignore them as well and just cast to ArrayList<String>. This could change in a future version, I guess.
Yeah it puts them in a map, but at some point it writes that map out into a string of some kind so it can be stored on disk. And whatever is doing THAT probably cares that it's one of the valid types.
|
0

If the list is of one type then you can convert the list to array using: toTypedArray() and after you can then check the type using: isArrayOf

But this would be inefficient since you are converting the list to array, better if you can just directly guess or retrieved the first item of the list.

2 Comments

I thought of retrieving the first item and checking, but the list might be empty
And the bigger problem is that toTypedArray needs to know the type of the collection in the first place. If you have an ArrayList<*>, you'll just get Array<Any>.

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.