Andresaurus
Andresaurus

Reputation: 7

How to create a mixed Array inside an Array?

I'm new to android and I need some help to create an mixed array inside an array.

This is what I need:

myArray = {["String", int, int, int], ["String", int, int, int], ["String", int, int, int]};

How can I create something like that? and how can I access to these variables later?

Thank you so much for your help.

Upvotes: 0

Views: 242

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81588

myArray = {["String", int, int, int], ["String", int, int, int], ["String", int, int, int]};

=> make a class to store the data

public class MyClass
{
    private String string;
    private int int1;
    private int int2;
    private int int3;

    public MyClass()
    {
    }

    public MyClass(String string, int int1, int int2, int int3)
    {
        this.string = string;
        this.int1 = int1;
        this.int2 = int2;
        this.int3 = int3;
    }

    public String getString()
    {
        return string;
    }

    public MyClass setString(String string)
    {
        this.string = string;
        return this;
    }

    public int getInt1()
    { 
        return int1;
    }
    ...
}

=> use collections

List<MyClass> list = new ArrayList<MyClass>();
list.add(new MyClass("Hello", 1, 2, 3);
list.add(new MyClass("World", 4, 5, 6);
MyClass mc = list.get(0);
for(MyClass myClass : list)
{
    android.util.Log.i(getClass().getSimpleName(), "Content: " + myClass.getString() + " " + myClass.getInt1() + " " + myClass.getInt2() + " " + myClass.getInt3());
}

Upvotes: 1

CQM
CQM

Reputation: 44308

You make an array of an object

that object contains variables for a String, int, int, int

ArrayList<MyObject> mArrayList = new ArrayList<MyObject>();

mArrayList.add(new MyObject("String1", 1, 2, 3));

Upvotes: 2

Related Questions