When do i use array lists over regular arrays? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When do i use array lists over regular arrays?

It seems like they serve the same function, but there must be some reason both exist. Is one more efficient than the other?

21st Jan 2019, 8:28 PM
Cole Deane
Cole Deane - avatar
3 Answers
+ 1
If I know how many numbers or whatever I want to save, I use arrays. Because you have to know the size of your array. int[] number = new int [5]; If I don't know it I use a list. ArrayList number = new ArrayList ();
21st Jan 2019, 8:38 PM
Denise Roßberg
Denise Roßberg - avatar
0
A list is also useful if you want to sort: https://www.sololearn.com/learn/Java/2183/?ref=app
21st Jan 2019, 8:41 PM
Denise Roßberg
Denise Roßberg - avatar
0
An Array is a static object, List are collections so you may program to interfaces as bestpractices recommends. For example: Given an array remove all elements when also exists at least one that is the double of it and return a sorted array of selectrd elements. This is better to wrap the array operate over it as a collection and then convert to array again.
3rd Feb 2019, 2:50 PM
Manuel Soto
Manuel Soto - avatar