0

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.

3
  • Do you really need an Array or can you use a List, or even a List of Lists? Commented Aug 5, 2014 at 17:58
  • 1
    Can you create an array of objects of a class you create to hold the String and 3 ints? Commented Aug 5, 2014 at 17:59
  • 3
    Since you already know structure "String", int, int, int of data you want to put in array why don't you create separate type (class) for it and make array of this class YourClass[]. Commented Aug 5, 2014 at 18:00

2 Answers 2

2

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

7 Comments

Thank you for your answer, but the answer from Zhuinden was more complete and helps me to understand better.
@Andresaurus, struggling makes you stronger; someone else writing the entirety of the code for you doesn't.
@Akshay Thank you so much, I didn't know that. I'm new at this web, and I think it's amazing. Thanks everyone for your help. PS: Sorry for my english. I Hope you can understand. =)
@ChiefTwoPencils I didn't write the entireity of the code though, I provided an example that provided information about multiple things that are necessary and essential to make it work and more, such as using List as static type and ArrayList as dynamic, constructors, setters that allow method chaining, foreach loop iteration, and the android logging mechanism because many people tend to not know about it. I see nothing wrong with providing information. If we were always stingy about how much we're willing to help newbies, no one would ever learn anything.
@Zhuinden, my comment wasn't directed @ you; I was pointing out what I believe to be true; you have the freedom to post what you like under the rules. It's not about being stingy, it's about drawing a line between teaching/guiding and hindering one's development, but more over the last sentence is an obvious logical fallacy.
|
1

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());
}

2 Comments

This answer helps me a lot to solve my problem. Thank you so much.
You're welcome! If you're curious to know what exactly is going on, I can add some extra comments so you'll know the terms (constructor, field encapsulation and getters setters, etc. Unless you actually know what those are, anyways.)

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.