Change item in ArrayList Java
How to Modify Element or Elements of an ArrayList in Java By Saruque Ahamed Mollick While working with ArrayList in Java, You might have faced some problems like, suppose you got a huge number of elements in your ArrayList and you have to modify a particular element from your list. Show So how to do that? In this post, we are going to learn the easy method to update or modify or you can say replace element as per your desire in Java program. To understand this I will go with an example. The list contains the following elements: [CodeSpeedy, ArrayList, Java]But you need the list like this one: So, you have to modify the last one. That means you need to change the last element which is Javawhose index number is 2. (As index number starts with 0) How To Convert An ArrayList to Array In Java Java Program To Update Element in an ArrayList:import java.util.ArrayList; public class Arraylistupdate { public static void main(String args[]){ ArrayListOutput: run: before modify: [CodeSpeedy, ArrayList, Java] after modify: [CodeSpeedy, ArrayList, J2EE] BUILD SUCCESSFUL (total time: 0 seconds)How to Increase and Decrease Current Capacity (Size) of ArrayList in Java Explanation: list.set(2, "J2EE");This is the main line which is responsible for updating an element. here 2 is the index ofJava. And then we putJ2EEso thatJavagot replaced byJ2EE. We added these because its a String type ArrayList. Finding an ArrayList in Java is Empty or Not Leave a Reply Cancel replyYour email address will not be published. Required fields are marked * Comment Name * Email * Please enable JavaScript to submit this form. |