Use of Array in Java

Use of Array in Java
Spread the love

The difference between array and arraylist is one of the major questions which is asked in the interviews if you are going for a job as a java programmer. This is a little bit tricky question but if you have your concepts clear, then it will not be a bard task for you. So to get more information about these arrays and arraylists, we need to learn a little deeper about these two terms and why are they used.

What is array and why it is used in java?

An array is a dynamically created object which serves as a container holding constant number of values which are of same type. When we declare an array, the memory space is allocated for all the values which are of same type. The array length must be specified before use and it remains constant. Coming to the uses of array is java; it is one of the commonly used objects in java as:

  • A particular and large number of groups of elements can be easily accessed by array name and element index.
  • Arrays are also useful when we need to do the large calculations in the loop.

What is arrayList in java and what are it uses?

It is a part of collection framework which provides us with dynamic arrays in Java. In arrayList you can give the dynamic memory if the collection grows or shrinks. The arrayList is present in java.util.package. It comes with various uses which are stated below:

  1. Java arrayList allows us to randomly access the list of elements.
  2. It inherits AnstractList class and also implements list interface.
  3. It works similar to the vector pointer in C++.

Now if go to the differences between these two objects in Java, then these are the following points:

  • Resizable

While array is of a specified size with a fixed length data structure that cannot be changed after creating the object in the program, but the arrayList gives us the advantage over array. ArrayList are dynamic in size in which you can add objects and its capacity grows automatically.

  • Primitive type

The arrayList does not contain the primitive types like int, float, and double. It only contains objects but in arrays, you can use both primitive types and objects.

  • Iterating the values

In the case of arrayList, we can use the iterator to iterate through it. In the arrays, we can use for and for each loop to iterate through arrays.

  • Type safety

With array one can ensure type safety through generics. As array contains homogenous data types, which means it will only contains the similar types of data types. Therefore, if you try to store the data of another type, it will throw ArrayStoreException.

  • Adding the elements

While adding the elements in arrayList, we have to use add() method whereas, in arrays, we insert the elements using the assignment operators.

  • Multidimensional

ArrayList are always single dimensional while the arrays can be multi dimensional.

Related posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.