Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java

The ArrayIndexOutOfBoundsException, also known as java.lang.ArrayIndexOutOfBoundsExcepiton is one of the most common errors in Java program. It occurs when Java program tries to access an invalid index e.g. an index which is not positive or greater than the length of an array. For example, if you have an array of String e.g. String[] name = {"abc"} then trying to access name[1] will give java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 error because index 1 is invalid here. Why? because index in Java array starts with zero rather than 1, hence in an array of just one element the only valid index is index zero.  This was one of the most common scenarios which cause several million of ArrayIndexOutOfBoundsException daily. Sometimes it just a plain programming error but sometimes if an array is populated outside and if there is an error on feed than also you get java.lang.ArrayIndexOutOfBoundsException in Java.

How to join two ArrayList in Java - Example

You can use the addAll() method from java.util.Collection interface to join two ArrayLists in Java. Since ArrayList implements List interface which actually extends the Collection interface, this method is available to all List implementation including ArrayList e.g. Vector, LinkedList. The Collection.addAll(Collection src) method takes a collection and adds all elements from it to the collection which calls this method e.g. target.addAll(source). After this call, the target will have all elements from both source and target ArrayList, which is like joining two ArrayList in Java. The second ArrayList will remain as it is but the first ArrayList on which you have added elements will have more elements. Its size will be equal to the sum of the size of first and second ArrayList.

How to read a text file into ArrayList in Java

Prior to Java 7, reading a text file into an ArrayList involves lot of boiler plate coding, as you need to read the file line by line and insert each line into an ArrayList, but from Java 7 onward, you can use the utility method Files.readAllLines() to read all lines of a text file into a List. This method return a List of String which contains all lines of files. Later you can convert this List to ArrayList, LinkedList, or whatever list you want to. Btw, this the fourth article in the series of reading a text file in Java. In the earlier parts, you have learned how to read a file using Scanner and BufferedReader(1). Then, reading the whole file as String (2) and finally reading a text file into array (3). This program is not very different from those in terms of fundamentals. We are still going to use read() method for Java 6 solution and will read all text until this method return -1 which signals the end of file.

What is the difference between byte and char in Java?

The byte and char are two numeric data types in Java and both can represent integral numbers in a range but there are very different from each other. The main difference between a byte and char data type is that byte is used to store raw binary data while other is used to store characters or text data. You can store character literal into a char variable e.g. char a = 'a'; A character literal is enclosed in single quotes. In terms of range, a byte variable can hold any value from -128 to 127 but a char variable can hold any value between 0 and 255. Another difference between byte and char in Java is that the size of the byte variable is 8 bit while the size of the char variable is 16 bit. One more difference between char and byte is that byte can represent negative values as well but char can only represent positive values as its range is from -128 to 127. In other words, a byte is a signed data type where the first byte represent the sign of number i.e. 0 for positive and 1 for a negative number, but char data type is unsigned. Let's see some more differences between byte and char in Java.

How to fix "variable might not have been initialized" error in Java

This error occurs when you are trying to use a local variable without initializing it. You won't get this error if you use a uninitialized class or instance variable because they are initialized with their default value e.g. Reference types are initialized with null and integer types are initialized with zero, but if you try to use an uninitialized local variable in Java, you will get this error. This is because Java has the rule to initialize the local variable before accessing or using them and this is checked at compile time. If compiler believes that a local variable might not have been initialized before the next statement which is using it, you get this error. You will not get this error if you just declare the local variable but will not use it.

How to find length/size of ArrayList in Java? Example

You can use the size() method of java.util.ArrayList to find the length or size of ArrayList in Java. The size() method returns an integer equal to a number of elements present in the array list. It's different than the length of the array which is backing the ArrayList, that is called the capacity of ArrayList. When you create an object of ArrayList in Java without specifying a capacity, it is created with a default capacity which is 10. Since ArrayList is a growable array, it automatically resizes when the size (number of elements in array list) grows beyond a threshold. Also, when an ArrayList is first created it is called empty ArrayList and size() will return zero. If you add elements then size grows one by one. You can also remove all elements from ArrayList by using a clear() method which will then again make the ArrayList empty as shown in our examples.

How to remove all elements of ArrayList in Java - Example

There are two ways to remove all elements of an ArrayList in Java, either by using clear() or  by using the removeAll() method. Both methods are defined in the java.util.List and java.util.Collection interface, hence they are available not just to ArrayList but also to Vector or LinkedList etc. Both elements removes all objects from ArrayList but there is a subtle difference in how they do. The clear() method is straightforward, it traverse through the ArrayList and sets all indices to null, which means the ArrayList becomes empty and all elements become eligible to Garbage collection, provided there is no more references to them. The time taken by clear() method is in O(n), which means the bigger the arraylist the longer it will take to empty it.