Dennis
Dennis

Reputation: 4017

Javascript multi-value array

This may seem like a rather odd/dumb question but I would like to replicate the following JavaScript array in Java.

var myArray = new Array();          
myArray[0] = ['Time', 'Temperature'];
myArray[1] = ['00:02', 20];
myArray[2] = ['01:30', 21];

What is strange to me is that there are multiple values in a single array location so I don't know what is going on; is this a two-dimensional array?

Upvotes: 0

Views: 218

Answers (2)

Vivin Paliath
Vivin Paliath

Reputation: 95518

You can do it in this manner:

String[][] myArray = {
    {"Time", "Temperature"},
    {"00:02", "20"},
    {"01:30", "21"}
};

Although I don't recommend this since you're losing a lot of type information and using String for everything doesn't scale well and isn't very maintainable. It is probably better to create a TemperatureReading class and have a list of TemperatureReading instances.

Upvotes: 1

Glenn Lane
Glenn Lane

Reputation: 4010

In Java:

Object[][] myArray = {
    new Object[]{"Time", "Temperature"},
    new Object[]{"00:02", 20},
    new Object[]{"01:30", 21}
};

Upvotes: 1

Related Questions