2

I want a 2D Matrix with one line of strings and the other line with int's. Is that possible? Or do I have to save the int's as strings and later convert them to int's again?

3
  • 5
    It's sort of possible, but it's usually a very bad idea. You could create an array of Object[][] and add String and Integers to it. But don't do this. If you need to connect a String and an int, create a class that does this, and then create a single dimensional collection or array of objects of this type. Commented Jun 25, 2013 at 11:53
  • 1
    Duplicate? stackoverflow.com/questions/5809486/… Commented Jun 25, 2013 at 11:55
  • Make a wrapper or use a HashMap Commented Dec 12, 2015 at 9:15

7 Answers 7

8

Rather use an object.

class MyEntry { 
    String foo;
    int number;
}
MyEntry[] array = new MyEntry[10];

But if you absolutely must for some unfortunate reason, you can use two types - only through an Object supertype.

Object[][] arr = new Object[2][10];

arr[0][0] = "Foo";
arr[1][0] = new Integer(50);
Sign up to request clarification or add additional context in comments.

Comments

3

No it is not possible . There can be only a single datatype for an array object. You can make a class having both the int and String as property and use it. Never use an Object[][] even if there is a temptation to do so, it is an evil workaround and hacks fail more than they succeeded . If Object was a sound technique then they wouldn't have introduced Generics for Collection !

4 Comments

But it can be a type that strings and Integers inherit from.
Integers inherit , int doesn't !
An Object[][] array for this is like shooting at ones own foot . Anyone suggesting to use it is wrong enough if my answer is wrong.
Integer will be functionally equivalent to int for this purpose. So the OP could use Integer objects, but regardless, that doesn't make his original suggestion a good idea.
1

You can create Objects 2D array and place there Strings and Integers, but I am not sure if it is good idea to have mixed types in arrays. You should probably describe your problem more so we could figure out better way.

Comments

0

You can create an array of the type Object and store any non-primitive Object in there. When you retrieve them, you'll need to make sure you check their class though.

if(objArray[0] instanceof String) {
    // do string stuff
} else if(objArray[0] instanceof Integer) {
    // do integer stuff
}

etc.

I think you're better off creating a new class that can store objects of the types that you want and just retrieve them using getters and setters. It's a lot safer and more stable.

Comments

0

You could do it if you do a 2D array of Object as in Object[][] myArray = new Object[x][y] where x and y are numbers.

All you would have to do is cast the Objects to their expected types before using them. Like (String) myArray[0][3] for example.

YOu should only do it this way if you know for certain what type the Object in a particular location will be.

However, it's generally not a good idea to do things this way. A better solution would be to define your own data structure class that has a String array and an int array as member variables. As in:

public class myData {
    String[] theStringArray;
    int[] theIntArray;

    public myData(String[] sArraySize, int[] iArraySize) {
        this.theStringArray = new String[sArraySize];
        this.theIntArray = new int[iArraySize);
    }

    ...
    // Additional getters / setters etc... 
    ...
}

2 Comments

No offense, but two separate arrays is even worse.
Probably you're right about that. My intent was mostly to illustrate that defining your own data structure would be better.
0

Just write :

// Declare and initialize a mixed 2D array
Object[][] matrix = {
    {"string1", "string2", "string3"},
    {1, 2, 3}
};

Comments

-1

Yes it is. If you declare as a Matrix of object then you can store string and Integer (not int), the difficulty will be after to retrieve them correctly :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.