Is ArrayList the same as array?

Lets Do It PL

Arrays vs ArrayList

With explanation and comparison in Java

M. Hamdi Ozdil
Follow
Jan 6 · 6 min read

Any person who has any experience with coding should have run into the Arrays term. But what are these Arrays we talking about? Why do we need them?

Arrays are basically Objects that hold a fixed number of values in a singular type. This means we can store numerical values like integers but we cannot store integer values with characters. But why do we need them? Actually, many companies have to store lots of information in their codes and do many manipulations on these.

In these times coding language creators or developers have to solve these problems and their response was the Array solution. With an array, we can store a lot of variables in the same object and call them whenever we need it easier than declaring them one by one.

But then what is an ArrayList? As we all know technology is growing faster than anything but new technologies need new tools and new ways to grow. Yes, Arrays are lifesavers for coding most of the time, or they were. But Arrays have some limitations and these usually cause problems. As we said before, coding language developers had to solve that problem and came up with ArrayList.

ArrayLists are very similar to Arrays but they have more freedom and are easier to use. Lets compare the two to understand better:

1-Capacity

When declaring an Array, we should specify the capacity or the compiler throws an error. If you noticed in the definition, there is a fixed number of values phrase. At first sight, someone might think What is the problem here? but it is usually a problem. Lets create one as an example:

int[] arrayOfSales = new int[1000];

Lets suppose, we are writing a code that stores all of our companys sales and we want to put all information into an Array because they are created for this purpose, right? For that, we created an Array that has high capacity but the question is: What is going to happen when we reach the maximum capacity? It becomes a disaster because usually we cannot guess these numbers and we actually should not. Arrays capacity isnt changeable later so it makes it harder to solve afterward.

Then how do developers fight these problems nowadays? The answer is: ArrayLists. Because ArrayLists have infinite capacity. Cool, isnt it? Lets solve the same problem with ArrayList.

ArrayList listOfSales = new ArrayList[];

Do we see any numbers? No, because there is no limit. We do not have to think about any maximum limit for our sales. What if we want to limit our sales? Then, we can declare an initial capacity in ArrayList or just use an Array.

2-Usage and Methods

Arrays and ArrayLists have the same purpose which is to store information. Arrays use the = operator for adding an element such as declaring a variable.

arrayOfSales[0] = 12;
arrayOfSales[1] = 25;

If we want to add a bunch of information then for loop is a good choice.

for[int i=0; i gives an error

An Array that stores only String cannot store an integer, it would obviously give an error. That makes Arrays type-safe data structures. But what about ArrayLists? Lets take a look:

ArrayList teachers = new ArrayList[];
teachers.add["Kenny"];
teachers.add[12]; //does this throw an error?

We created an ArrayList object without using Generics. This creates an ArrayList with no restriction to any type. Therefore, that code does not throw any errors. Again, it might seem like an advantage but no, it makes ArrayLists uncomfortable to use. Lets suppose, we would like to do some calculations on that ArrayList and we do not know which element is in which type. We cannot perform any mathematical calculation with Strings so we have to use Generics to solve this issue:

ArrayList teachers = new ArrayList[];
teachers.add["Levi"];
//teachers.add[24]; --> gives an error

Thats it. Now, our ArrayList has a restriction to have only a single type to store. Thats how we should use it because type-safety is an important factor that developers should seek.

Summary

Arrays and ArrayLists can be alternatives to each other but most of the developers stopped using Arrays mostly because of the size problem that they bring. Performance and other problematic sides of ArrayLists are completely ignored because they are less complex and easier to use. In many applications, ArrayLists are preferred to use because they save much time during the development phase and let developers break their limits. They both have some advantages and disadvantages and the decision is always up to the developer.

Video liên quan

Chủ Đề