I want to know how to add or append a new element to the end of an array. Is any simple way to add the element at the end? I know how to use a StringBuffer but I don\'t know
Arrays in Java have a fixed length that cannot be changed. So Java provides classes that allow you to maintain lists of variable length.
Generally, there is the List
interface, which represents a list of instances of the class T
. The easiest and most widely used implementation is the ArrayList
. Here is an example:
List words = new ArrayList();
words.add("Hello");
words.add("World");
words.add("!");
List.add()
simply appends an element to the list and you can get the size of a list using List.size()
.