Monday, 20 November 2017

What is array data structure in Java? Properties, Example and Tutorial

Without any doubt, an array is one of the most used data structure in all programming language, including Java. Pick up any programming language be it functional, object-oriented, imperative or even scripting languages like Python, Bash, and Perl, you will always find array. That's why it's important for any programmer to have a good understanding of  array data structure. The array is used to store elements in the contiguous memory location and many C, C++ programmer can take advantage of a pointer to work with an array. In Java, there are no pointers and arrays are also a little bit different. They are the object, they have length field which denotes how many elements array can store. Arrays are created in the special memory area called heap memory in JVM, which is also created when you start the JVM. What remains same is that you can access the array element in constant time using their index, this works almost similarly in both C, C++ and Java, they start with 0 and ends at length -1, but Java array has an extra caveat that arrays index access are subject to bound check in Java.

Friday, 17 November 2017

How to Compare Two Arrays in Java - String, Integer Array Example

Our task is to compare two arrays in Java and see if they are equal to each other or not. Of-course we can't compare an String array to an int array, which means two arrays are said to be equal if they are of same type, has same length, contains same elements and in same order. Now, you can write your own method for checking array equality or take advantage of Java's rich Collection API. Similar to what we have seen while printing array values in Java, java.util.Arrays class provides convenient methods for comparing array values. They take care of all four condition, I have mentioned above. In fact, Arrays class also provide deepEquals() method to compare two-dimensional array in Java. By the way it's not restricted to just two-dimensional and also valid for any multi-dimensional array.

Tuesday, 14 November 2017

2 Ways to Remove Numbers from Integer Array in Java?

I have showed you how to reverse an array in place in Java and now I have come back with another array based coding interview question. It's also one of the frequently asked coding questions, not as popular as the previous one but still has been asked lot of times. Let's see the problem statement first.

Problem: Given an array and a value, write a function to remove all instances of that value from the array in Java (in place) and return the new length, or simply print out the array before and after removing the number. The order of elements can be changed. It doesn't matter what you leave beyond the new length. For example, if the given array is {12, 122, 12, 13, 14, 23, 14}, and number to be removed is 12 then the result array should be {122, 13, 34, 23,14}, and the length of the new array is 5.

Monday, 13 November 2017

How to convert list to array in Java and vice-versa

There is no easy way to convert an array to list in Java, but you can easily convert a list into array by calling toArray() method, which List inherit from Collection interface. If you solely rely on core JDK, then only way to convert an array to list is looping over array and populating list one element at a time. But if you can use open source libraries like Google Guava or Apache Commons lang then there is many utility classes to convert list to array and vice-versa. If you are working on Java application, you will often need to convert between list and array. A list is nothing but a dynamic array which knows how to re-size itself when it gets full or get close to full. List uses load factor to decide when to re-size, default value of its is 0.75. When they re-size, list usually double their slots e.g. goes from 16 to 32 etc. You can find these nifty details in their implementation classes e.g. ArrayList is one of the popular list in Java which provides order and random access. BTW, if you want to truly master Java Collection framework, then you must read Java Generics and Collection book, written by Maurice Naftaline and one of the must read book to become expert on Java Collections framework.

Friday, 10 November 2017

How to declare and Initialize two dimensional Array in Java with Example

An array of more than one dimension is known as multi-dimensional array. Two of the most common examples of multi-dimensional arrays are two and three dimensional array, known as 2D and 3D array, anything above is rare. I have never seen 4 dimensional arrays, even 3D arrays are not that common. Now question comes, when do use multi-dimensional array? Any real life example? Well, 2D arrays are very common on platform games like Super Mario Bros to represent screen or terrain; 2D arrays can also be used to represent structures like spreadsheet, or to draw board games like Chess, which requires 8x8 board, Checkers and  Tic-Tac-Toe, which requires 3 rows and 3 columns. Another popular application of multi-dimensional arrays are in matrix manipulation. For example to represent a 3x3 matrix you need a two dimensional array of 3 one dimensional array each containing 3 elements.