+ 3

What is the use of ArrayList in java?

2nd Aug 2017, 2:20 PM
Viraj Singh
Viraj Singh - avatar
3 ответов
+ 4
@Prakhar Eh, for loops are being used with arraylists anyway. Sorting is also not any different with arrays. If anything, it's probably easier syntax wise to use arrays (Just for sorting). @Voraj ArrayLists are stronger than arrays when adding or removing elements that would make you want to change the length of the array. ArrayLists use an array internally that has a larger capacity than size, making adding to the list a very fast operation. (Until the list is full) Grabbing data is also very fast with arraylists. So, use ArrayLists instead of expanding an arrays size if you need to add elements to the list all the time (and use them). If you don't want to get the data and are just adding/removing, use a linked list.
2nd Aug 2017, 2:43 PM
Rrestoring faith
Rrestoring faith - avatar
+ 4
@Rrestoring faith is right, if you for example do not know how many elements your collection will have, you can take an ArrayList because of the possibility to add easily elements while arrays have a fixed size.
2nd Aug 2017, 5:39 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
hi, array list is a better way of creating an array than using the <datatype> [] array. By using it the elments can be sort easily into increasing/decreasing order. Also the elements can be accessed easily by easy syntax. Not by using a for.. loop. ArrayList is a predefined class in Java util package.
2nd Aug 2017, 2:25 PM
Prakhar Saxena
Prakhar Saxena - avatar