0

I am new to Android. I wonder how to create an arraylist with multiple elements. I have a list like:

list1 = ["Circle", Color.WHITE, Color. BLACK, (20,20), (30,30), (40,40)];

This array contains String, int(the color) and coordinates. Is that available to put all of them into an arraylist. If so, how can I merge two lists together. Also, how can I get the value from it? I want to generate some list like above and do some code to merge(combine) them. Thanks!

1

2 Answers 2

1

It's not recommended to put different types into an arraylist. I think the best solution would be to put all your data (the String "Circle", the colors and the coordinates) into a new class and have an ArrayList with objects of this new class.

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

1 Comment

Could you tell me how to do it or some links? Thank you.
0

You could do this by declaring a List<Object>.

List<Object> randomBagOfJunk = new ArrayList<Object>();
randomBagOfJunk.add("Circle");
randomBagOfJunk.add(Color.WHITE);
randomBagOfJunk.add(Color.BLACK);
// no idea what a (n, n) is...

I strongly recommend that you don't do this, however. Find a different way to store the information. List<Object> is almost never the right solution, unless it's storing objects whose class is Object.class.

Create a new class to hold your data, and you can write a merge() method there as well.

class RandomBagOfUsefulThings
{
    String name;
    Color foreground;
    Color background;
    // etc.

    RandomBagOfUsefulThings merge(RandomBagOfUsefulThings other)
    {
        // snip...
    }
}

1 Comment

I have no idea what output you want from a list "merge" operation.

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.