Is it possible to make an array with two data types in android programming. I want to make an array that hold Bitmap and a number is that possible, if not is there any other way to do this?
3 Answers
- If one of them is a key, for example - the number is the id of the bitmap, you can use a Map.
- If they are related but not unique, you might want to create a class that holds both of them and create an array of it.
- If you just want an array that holds both, you can use
ArrayList<Object>, although in this case you might want to look into your design once again.
2 Comments
AAnkit
Perfect, Was typing the same, now +1 instead of posting duplicate answer:)
Blake Gibbs
I'm making a simple game so, i have 4 bitmaps and easch of them have their powers so i want have that for example Green ball have power with number 1, so i want that array to hold two object can you make an example because i have never worked with lists :)
Sure, use an array of Object type.
Should you? Probably not–why store two different things in the same array? Should you be using a map?
If the bitmap and number are tightly coupled, create an object that encapsulates them both, and create an array of that new type. Otherwise your array operations will require type checking manually.
You might also want to consider using a collection instead, like an ArrayList, instead of an Array.
4 Comments
Dave Newton
@edem That makes zero sense; that's how you hold two different object types that don't share a superclass or interface: you either create an untyped collection, or a collection of object. You should have voted down Binyamin, too, then.
Alexander
@edem Why Object type can not be mentioned
Dave Newton
@edem No worries :) Just confused me.
Adam Arold
I read your answer again and I realized that I misinterpreted it.