ArrayList es parte del marco de la colección y está presente en el paquete java.util. Nos proporciona arreglos dinámicos en Java. Sin embargo, puede ser más lento que las arrays estándar, pero puede ser útil en programas donde se necesita mucha manipulación en la array. Esta clase se encuentra en el paquete java.util .
Java
// Java program to demonstrate the // working of ArrayList in Java import java.io.*; import java.util.*; class ArrayListExample { public static void main(String[] args) { // Size of the // ArrayList int n = 5; // Declaring the ArrayList with // initial size n ArrayList<Integer> arrli = new ArrayList<Integer>(n); // Appending new elements at // the end of the list for (int i = 1; i <= n; i++) arrli.add(i); // Printing elements System.out.println(arrli); // Remove element at index 3 arrli.remove(3); // Displaying the ArrayList // after deletion System.out.println(arrli); // Printing elements one by one for (int i = 0; i < arrli.size(); i++) System.out.print(arrli.get(i) + " "); } }
Java
// Java Program to Add elements to An ArrayList // Importing all utility classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating an Array of string type ArrayList<String> al = new ArrayList<>(); // Adding elements to ArrayList // Custom inputs al.add("Geeks"); al.add("Geeks"); // Here we are mentioning the index // at which it is to be added al.add(1, "For"); // Printing all the elements in an ArrayList System.out.println(al); } }
Java
// Java Program to Change elements in ArrayList // Importing all utility classes import java.util.*; // main class class GFG { // Main driver method public static void main(String args[]) { // Creating an Arraylist object of string type ArrayList<String> al = new ArrayList<>(); // Adding elements to Arraylist // Custom input elements al.add("Geeks"); al.add("Geeks"); // Adding specifying the index to be added al.add(1, "Geeks"); // Printing the Arraylist elements System.out.println("Initial ArrayList " + al); // Setting element at 1st index al.set(1, "For"); // Printing the updated Arraylist System.out.println("Updated ArrayList " + al); } }
Java
// Java program to Remove Elements in ArrayList // Importing all utility classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating an object of arraylist class ArrayList<String> al = new ArrayList<>(); // Adding elements to ArrayList // Custom addition al.add("Geeks"); al.add("Geeks"); // Adding element at specific index al.add(1, "For"); // Printing all elements of ArrayList System.out.println("Initial ArrayList " + al); // Removing element from above ArrayList al.remove(1); // Printing the updated Arraylist elements System.out.println("After the Index Removal " + al); // Removing this word element in ArrayList al.remove("Geeks"); // Now printing updated ArrayList System.out.println("After the Object Removal " + al); } }
Java
// Java program to Iterate the elements // in an ArrayList // Importing all utility classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating an Arraylist of string type ArrayList<String> al = new ArrayList<>(); // Adding elements to ArrayList // using standard add() method al.add("Geeks"); al.add("Geeks"); al.add(1, "For"); // Using the Get method and the // for loop for (int i = 0; i < al.size(); i++) { System.out.print(al.get(i) + " "); } System.out.println(); // Using the for each loop for (String str : al) System.out.print(str + " "); } }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA