0

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?

1
  • Do you want to associate a number to each bitmap? Commented Jul 29, 2012 at 17:11

3 Answers 3

7
  • 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.
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, Was typing the same, now +1 instead of posting duplicate answer:)
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 :)
5

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

@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.
@edem Why Object type can not be mentioned
@edem No worries :) Just confused me.
I read your answer again and I realized that I misinterpreted it.
0

Array hold same type data but List can hold different type data.

So you can use List type collection such ArrayList, LinkedList,Stack, Vector

Comments

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.