I’m doing a project, where I’m reading accelerometer sensor data from a smartwatch. The app that is made has a start and stop button, that starts and stops the sensor readings, respectively.
I would like to start recording the data when the user presses start – I know how to access the sensor data, but I need a way to store the data until the stop button is pressed. Accelerometer data is X-, Y-, and Z-axis data, that could look something like this:
[2.034343, 8.342423, 0.012313]
So I need a way to store this type of vector within an additional array, in order to separate each vector recorded. Thus, the data structure I’m looking for should look something like this when having recorded three vectors of data:
[[2.034343, 8.342423, 0.012313], [6.031843, 2.349153, 0.012313], [1.734843, 5.342423, 1.012393]]
The data structure will be filled with these vectors in the time between the user pressing start to when stop is pressed.
Do I need an ArrayList of ArrayLists? Or do you have any other suggestions as to what the best data structural solution to this is? As of right now, the data only needs to be stored as long as the app is running.
I have no idea why I cannot figure out how to do this in Kotlin, I have only been able to make an ArrayList of ArrayList, however, instead of separating each vector with the brackets (as shown above) it just appends all the vector values to an array that holds all values (like a standard array).
Hope I can get some help. Thanks in advance.
2
Answers
If you want to do this with a 2D structure, you can instantiate an ArrayList or MutableList, and then you just add lists to it each time you have new data. If you use ArrayList specifically, you can specify the initial size. Setting a large initial size can help avoid the list having to resize itself while you’re adding data, which can be an expensive operation if it has become large.
However, I would probably use a 1D collection because all the data points are Floats (or Doubles?) and it’s easy to read them 3 at a time. Then you aren’t having to create a new collection object to wrap each data point, which requires more memory allocation.
Normally, I wouldn’t do something like this, which sacrifices code clarity and conciseness, but since you’re recording a stream of data that could be very large, it makes sense to optimize it a bit up front.
A data class could make sense: