Search code examples
javaarraysstringmatrixint

Making a mixed 2D array in java


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?


Solution

  • 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);