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?
-
It's an array of arrays. That's kind-of like a two-dimensional array, but it's not stored the way a 2D array would be stored in C or C++.Pointy– Pointy2013年11月10日 20:02:55 +00:00Commented Nov 10, 2013 at 20:02
2 Answers 2
In Java:
Object[][] myArray = {
new Object[]{"Time", "Temperature"},
new Object[]{"00:02", 20},
new Object[]{"01:30", 21}
};
Comments
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.