Array List
Notes on array list
import java.util.ArrayList;
abstract class ArrayListDemo{
ArrayList<String> alphabet = new ArrayList<String>(Arrays.asList("a", "b", "c"));
public ArrayListDemo() {
}
protected abstract void changes();
}
Add, edit, and remove elements
To add elements, use the add()
method. To add an element at a specific index, use add(index, element)
. To add all elements in an array to another array, use addAll(index, array)
. To edit elements, use set(index, element)
. To remove elements at a specific index, use the remove(index)
method, or to remove a specific element, use remove(element)
. To clear an array, use the clear()
method.
public class SpecificArray extends ArrayListDemo {
public SpecificArray() {
}
public void changes() {
super.alphabet.add("d");
super.alphabet.add(1, "z");
System.out.println(super.alphabet);
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
public class SpecificArray extends ArrayListDemo {
public SpecificArray() {
}
public void changes() {
super.alphabet.set(0, "z");
System.out.println(super.alphabet);
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
public class SpecificArray extends ArrayListDemo {
public SpecificArray() {
}
public void changes() {
super.alphabet.remove(1);
super.alphabet.remove("c");
System.out.println(super.alphabet);
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
public class SpecificArray extends ArrayListDemo {
ArrayList<String> alphabet2 = new ArrayList<String>(Arrays.asList("z", "x", "y"));
public SpecificArray() {
}
public void changes() {
super.alphabet.addAll(1, alphabet2);
System.out.println(alphabet);
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
public class SpecificArray extends ArrayListDemo {
public SpecificArray() {
}
public void changes() {
super.alphabet.clear();
System.out.println(alphabet);
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
Array Info
To find the size of the array, use the size()
method. To see the specific element at an index, use the get()
method. indexOf(element)
will return the first index that the element is in, while lastIndexOf(element)
will return the index of the last occurrence of the element. To find the associated hash of the array, use the hashCode()
method. The isEmpty()
method will return true
if the array is empty, and false
if not. To check if the array contains an element, use the contains(element)
method. To check if two arrays are equal to each other, use the equals(array)
method, and to check if an array contains all of the elements in a collection, use the containsAll(array)
method. To sort the elements in an ArrayList, use the sort()
method.
public class SpecificArray extends ArrayListDemo {
public SpecificArray() {
}
public void changes() {
System.out.println(super.alphabet);
System.out.println("Size of alphabet array is: " + super.alphabet.size());
System.out.println("Element of index 0 is: " + super.alphabet.get(0));
System.out.println("Index of element b is: " + super.alphabet.indexOf("b"));
super.alphabet.add("a");
System.out.println("Updated ArrayList: " + super.alphabet);
System.out.println("Last occurence of a is at index: " + super.alphabet.lastIndexOf("a"));
System.out.println("Hash code of alphabet is " + super.alphabet.hashCode());
if (super.alphabet.isEmpty() == true) {
System.out.println("alphabet array is empty!");
} else {
System.out.println("alphabet array is not empty!");
}
if (super.alphabet.contains("z") == true) {
System.out.println("alphabet array contains the element z");
} else {
System.out.println("alphabet array does not contain the element z");
}
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
public class SpecificArray extends ArrayListDemo {
ArrayList<String> alphabet2 = new ArrayList<String>(Arrays.asList("z", "x", "y"));
public SpecificArray() {
}
public void changes() {
System.out.println("alphabet: " + alphabet);
System.out.println("alphabet2: " + alphabet2);
if (alphabet.equals(alphabet2)) {
System.out.println("alphabet and alphabet2 are the same");
} else {
System.out.println("alphabet and alphabet2 are different");
}
if (alphabet.containsAll(alphabet2)) {
System.out.println("alphabet and alphabet2 have the same elements");
} else {
System.out.println("alphabet and alphabet2 have different elements");
}
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);
public class SpecificArray extends ArrayListDemo {
public SpecificArray() {
}
public void changes() {
alphabet.add("a");
System.out.println("Modified alphabet ArrayList: " + alphabet);
alphabet.sort(Comparator.naturalOrder());
System.out.println("Sorted ArrayList alphabet: " + alphabet);
}
public static void main(String[] args) {
SpecificArray changeArray = new SpecificArray();
changeArray.changes();
}
}
SpecificArray.main(null);