Category Archives for General

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:

 

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.

Sort ArrayList in Java with code example

How to sort ArrayList in Java? This is a common need whenever you have to deal with a situation that needs to keep order of items.

There are two ways to sort ArrayList in Java. In the first approach, we will call Collections.sort() and the elements will be sorted by its natural order. Second approach is used for custom sorting and is done with the use of a Comparator class and comparable function.

Sort by natural order

The data types have a natural order. For example, number go like 0, 1, 2 …, the alphabets a,b,c.. etc. So, there is default sort implementation in Java that will sort the data elements by its natural order. Thus this approach works for data types that represent numbers and String.

Example to sort ArrayList that contain Integers

output:

The same approach will work for other Data types such as Double and String.

Here is the example with Double

output:

Sorting Array List in the descending order

You can sort in descending order in two different ways

  1. Call Collections.reverse() after a Collections.sort()
  2. While doing a Collections.sort(), pass a reverse Comparator. This Comparator you will get by calling Collections.reverseOrder().
Output
 

Extremely Simple Java Tutorial – Chapter 8

Chapter 8: How to use Class and Objects

Now you have everything to get started with Java. You should be able to write small programs in Java and play around with it. However, there is one more section I would like to cover before we wind up this basic course. That is Java Classes, Objects and the usage. In the next level of courses we will get in to even more details. However you should know how Java class and Object could be utilized. We are just going to focus on the usage without worrying about the technicality about it. You can read about Object Oriented Concepts for a quick understanding. If you do not understand everything- do not worry about every details of it. That is not required at this point.

What is class and object?

Relating to real life, class is simply a generic concept. Furniture, Human, Animal – these are all examples of class.

Object as the name implies is something that really exists and you can touch and feel. A Table, Gandhi, a Lion these are examples of objects corresponding to the class we discussed above.

Classes give us functionality to be used. In Java there are many prebuilt classes. We will discuss couple of them to see how they could be used.

We will focus on String and Integer class.

 

String class represents a string of literals. For example “This is my place” is a String.

Here is a java example using String.

 

Output here will be:

This is my place

my place

Here when we look at it, String is giving us a function() called substring. We have used this function to get substring of the original String and we got “my place”.

String also gives you a function called length() which we used in the substring function.

Exercise:

  1. There is a function called chartAt() in String class. Use this function and print out the value. Do a web search – this will help you learn how to find various functions in a java library
  2. Identify 3 more functions in String and use it in your program.

 

Initialization of an Object

Just like physical objects, Java objects also need space. Java Objects sits in computer memory. So we say objects needs to be initialized in memory.

We did this in the line

String sample = “This is my place”;

When we did this, we allocated space for the String variable ‘sample’ and assigned the value of “This is my place”.

Proper way to allocate memory is by using new() operator. Remember addition operator? Similarly we have an operator to create new objects. It is called new().

So we can also rewrite the above line like below:

 

String sample = new String(“This is my place”);

For many reasons such as better performance, java treats String as special and you can simply assign value directly to it without using new operator.

 

If you want to create an Integer object, you will use something like below

Integer intVar = new Integer(10);

Once you have created the object, you can start using the variable, intVar.

 

Exercise:

  1. Identify functions in Integer class, and use it in a program.

Conclusion

 

Congratulations!!!

You have successfully completed the first course in Java. Now it is the time to play around with the language and be more proficient. More you try better you will get.

More tutorials will be added in Java Learning Academy. So you might want to keep checking JLA.

Also, sign up JLA mailing list so that the updates will come to your inbox automatically. You can do it here:

http://www.javalearningacademy.com/signup

< Previous Chapter

1 2 3 6