ArrayList to String Array Conversion in Java

There are essentially three ways to convert ArrayList that contains String objects to convert in to String Array. Since ArrayList can contain any object, even though String object type is used in this example, this is equally valid for other data types as well.

First two methods are covered just for the completion. For practical purposes, you should be able to use just the third method.

Method 1:

This is a very basic method, it is essentially about creating an Array of the type String and the size of the list, then loop through the list and add one element at a time.

Method 2:

toArray() method will return Object Array. From the Object Array you can use Arrays.copyOf method to copy in to a String array

Method 3:

Use toArray() method with String Array as argument.

Below is the sample code for all the methods

Display:

 

Array List add at index Java Example

ArrayList is a dynamic array. It means it can grow as you add items in to it. If required, you can convert array in to ArrayList easily. There are time, the programmer wants to add an item at certain index. This is possible in ArrayList.

ArrayList has an overload add method that takes index. The signature would be ArrayList.add(index, Object).

When you add an object at an index, the item currently at the position gets shifted to accommodate this change. This is shown in diagram as below.

ArrayList with three items

Java ArrayList Before Add

The same ArrayList after, ‘NewItem’ has been added

Java ArrayList After Add

Here is code example for that.

If you all this function from a method, here will be the output:

 

Simple Example of Polymorphism in Java

Polymorphism is a very interesting Object Oriented Concept. Let me explain Polymorpshims in Java with a simple example.

What does Polymorphism mean?

Poly stands for many. And morph stand for shape. Thus polymorphism simply mean many shapes.

If you wonder many shapes of what? In the context of object orientation it means many shapes of the same function. I have covered object orientation concepts as a story in another post. Please read that, it will give you an overview.

We are going to take that example here and elaborate with the code.

Let us assume :

There is a Dad class and Son class. Son class is derived from Dad class.

Dad class has a function called whoAreYou(). Son class overrides this function and also has the function whoAreYou(). Now the same function will return different result based on the context. That is Polymorphism.

 

Dad class:

Son class:

Main class:

 

Output:

 

That is a simple example of polymorphism in Java.

Note: Java programmers are told that there are two ways to achieve polymorphism – overriding and overloading. Overloading means having the same method name with different argument types or different number of arguments. Polymorphism by overloading is contested. When you have same method name but different argument, eventually is it a totally different function. However, some programmers call it a static polymorphism. When you are designing the object model, you don’t have to worry about this debate. But good to know for theoretical knowledge. You may want to check out here for more details on this.

ArrayList Object Sort in Java with Comparable and Comparator

Sorting ArrayList that contains custom objects need a different approach than, a ArrayList with primitive types. Sorting ArrayList with basic types is easy.

When it has custom objects, we need to specify what you really mean by sorting. For example, a User object might have properties age and name. When you are sorting on Users, do you want to sort by age or name?

This is why we need to have our own way to mention the sorting mechanism.

Two approaches could be taken here.

Sort with Comparable Interface

If you can implement Comparable interface on the Class to be sorted, it will force you to have compareTo() method. Here is where you mention your sorting mechanism.

The following code shows sorting with Comparable Interface. We are sorting objects of User class.

User class:

Main:

Output:

See the result sorted by age.

 

Sort with Comparator

So why we need Comparator? If you look in to the Comparable mechanism, you might have noticed that you can do it only for a single property and that has to be an integer.

With Comparator we have even more flexibility and we can compare based on various properties.

In the following example, we will sort User objects by age and name.

Approach is to create two comparators one for age and another for name. In the Collections.sort, we will pass appropriate Comparator.

User class:

Main:

 

Output:

 

ArrayList with different Object Types

Keeping different types of objects in the same ArrayList object could be done by using Object generic type.

The idea here is to allow ArrayList to take any Object then check the actual type at the time of retrieving it.

The following code explains this:

Output:

Similar code samples:

Sorting ArrayList of primitive types

1 2 3 10