Array Lists and Strings in Java
Array Lists and Strings in Java
STRINGS IN JAVA
ARRAY LISTS
import java.util.ArrayList;
class Main {
public static void main (String[] args) {
// Creating an ArrayList
ArrayList<Integer> a = new ArrayList<Integer>();
// Printing ArrayList
System.out.println(a);
}
}
SOME KEY POINTS OF ARRAYLIST IN JAVA
• Both of them doing the same task but clear() is faster than removeAll().
SOME BUILT IN METHODS OF ARRAY LISTS
• arraylist.get(1);
//get method takes a single parameter, position of the element and returns the elements
present in the specific position
SOME BUILT IN METHODS OF ARRAY LISTS
• arraylist.indexOf(Object obj)
//The indexOf() method takes a single parameter, obj - element whose position is to be
returned.
arraylist.sort(Comparator.naturalOrder());
// The sort() method takes a single parameter, comparator - specifies the sort order of the
arraylist
// The sort() method does not return any value. Rather it only changes the order of
elements in an arraylist.
arraylist.sort (Comparator.reverseOrder());
JAVA STRINGS
• The String class is one of the most fundamental types in Java, designed to represent
immutable sequences of characters. Here's a closer look at its characteristics and the
interfaces it implements:
• Immutability: Once instantiated, a String object cannot be modified. This immutable
design is a deliberate choice to ensure thread safety, consistency, and efficiency, especially
regarding the String pool mechanism.
• String Pool: Java maintains a pool of string literals to help save memory. When a new string
literal is created, Java checks the Pool for a matching string. If found, the new variable
references the pooled string. If not, the new string is added to the Pool.
STRINGBUFFER
} }
SOME BUILTIN METHODS FOR STRINGS