Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 475
tortilla Avatar asked Dec 13 '25 22:12

tortilla


1 Answers

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);
like image 50
Ondra Žižka Avatar answered Dec 15 '25 12:12

Ondra Žižka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!