0

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?

1
  • 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++. Commented Nov 10, 2013 at 20:02

2 Answers 2

1

In Java:

Object[][] myArray = {
    new Object[]{"Time", "Temperature"},
    new Object[]{"00:02", 20},
    new Object[]{"01:30", 21}
};
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.