0

I'm trying to code a realtime drawing App like the sample one Google has

https://github.com/googlearchive/AndroidDrawing

but I am curious to why they chose Arraylist<Point> data structure for adding points to the screen when drawing on the canvas. (It's adding about 100 Point objects when I draw a line )

https://github.com/googlearchive/AndroidDrawing/blob/master/app/src/main/java/com/firebase/drawing/Segment.java

The list of points represent one line.

Since an ArrayList is an implementation of a dynamic array. You have to constantly add new entries (points in this case) to the back of the container. Dynamic arrays have to be reallocated every time you change their size which means the entire contents have to be copied to a new block of memory. Linked lists, on the other hand, allow you to insert or remove entries without having to reallocate the rest of the list. Linked lists are much faster than dynamic arrays when you do frequent modification of the elements like you're doing here.

Why not use a LinkedList?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • Samples are just samples, not best practice. That said, [When to use LinkedList over ArrayList in Java?](https://stackoverflow.com/q/322715/295004) – Morrison Chang Jan 22 '21 at 21:51
  • yes I've seen that question but everyone in that question is saying to not use a LinkedList. But using a LinkedList in this case makes sense...so it's a bit conflicting on which to use. – DIRTY DAVE Jan 22 '21 at 21:52
  • 1) write the app 2) do performance measurements to see if it matters. Due different experiences and having to deal with different problems, you'll get multiple answers. Only if its solving your problem does it make a difference. If you feel LinkedList is more appropriate for your problem, do that, or ArrayDeque. – Morrison Chang Jan 22 '21 at 22:01

0 Answers0