I'm looking to create a 2D String 'matrix' object in Java. My two goals with this are for efficiency and code simplicity.
I've heard that using an ArrayList is quite a bit more efficient than a String[][]. Firstly, I'm wondering if that's true, and if so, how much more efficient?
The other thing is that I must have the ability to add columns and rows to the 'matrix'. I was able to develop some efficient algorithms for adding rows and columns to a String[][]. I'm curious to know if it would be worth it to develop algorithms to manipulate a 2D List in this way - would there be a significant performance improvement?
Thanks for your help!
First the simple answer: Direct array operations will be faster since you avoid the overhead of the general purpose classes. You can even gain more performance by leveraging the Unsafe instance (non public API) for direct array access.
And for the opinion part: I would rather look at how things can be executed concurrently or distributed amongst different systems if performance is that important. In that case I would prefer avoiding using arrays directly since that would be hard to maintain. But of course it all depends on your specific use case.
So, like with all performance related questions, do some measurements for your specific use case and decide for yourself what works best for you.
And like I said in the comments, I have fiddled around with own Matrix implementation which have two specialised Map implementations (CompactArrayMap and DirectArrayMap) which are both int to key mappings on the keys natural order, so much like an array but with map functionality.