YouTip LogoYouTip

Java Arraylist

[![Image 1: Java Collections Framework](#) Java Collections Framework](#) ArrayList class is a dynamically modifiable array, unlike ordinary arrays, it has no fixed size limit, we can add or delete elements. ArrayList inherits AbstractList and implements the List interface. !(#) The ArrayList class is in the java.util package and needs to be imported before use. The syntax is as follows: import java.util.ArrayList; // Import ArrayList ClassArrayList objectName =new ArrayList(); // Initialization * **E**: Generic data type, used to set the data type of objectName, **can only be a reference data type**. * **objectName**: Object name. ArrayList is an array queue, providing related add, delete, modify, traverse and other functions. ### Adding Elements ArrayList class provides many useful methods, to add elements to ArrayList you can use the add() method: ## Example import java.util.ArrayList; public class TutorialTest { public static void main(String[] args){ ArrayList sites =new ArrayList(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Weibo"); System.out.println(sites); } } In the above example, the output is: [Google, Tutorial, Taobao, Weibo] ### Accessing Elements To access elements in ArrayList, you can use the get() method: ## Example import java.util.ArrayList; public class TutorialTest { public static void main(String[] args){ ArrayList sites =new ArrayList(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Weibo"); System.out.println(sites.get(1));// Access the second element } } **Note**: Array index values start from 0. In the above example, the output is: Tutorial ### Modifying Elements To modify elements in ArrayList, you can use the set() method. The set(int index, E element) method's first parameter is the index, indicating the position of the element to replace, and the second parameter is the new element (element), indicating the new value to set: ## Example import java.util.ArrayList; public class TutorialTest { public static void main(String[] args){ ArrayList sites =new ArrayList(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Weibo"); sites.set(2, "Wiki");// The first parameter is the index position, and the second is the value to be modified System.out.println(sites); } } In the above example, the output is: [Google, Tutorial, Wiki, Weibo] ### Deleting Elements To delete elements in ArrayList, you can use the remove() method: ## Example import java.util.ArrayList; public class TutorialTest { public static void main(String[] args){ ArrayList sites =new ArrayList(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Weibo"); sites.remove(3);// Remove the fourth element System.out.println(sites); } } In the above example, the output is: [Google, Tutorial, Taobao] ### Calculating Size To calculate the number of elements in ArrayList, you can use the size() method: ## Example import java.util.ArrayList; public class TutorialTest { public static void main(String[] args){ ArrayList sites =new ArrayList(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Weibo"); System.out.println(sites.size()); } } In the above example, the output is: 4 ### Iterating Through ArrayList We can use for to iterate through elements in the ArrayList: ## Example import java.util.ArrayList; public class TutorialTest { public static void main(String[] args){ ArrayList sites =new ArrayList(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Weibo"); for(int i =0; i < sites.size(); i++){ System.out.println(sites.get(i)); } } } In the above example, the output is: GoogleTutorialTaobaoWeibo You can also use for-each to iterate through elements: ## Example import java.util.ArrayList; public class TutorialTest { public
← Java LinkedlistJava Enum β†’