• Register
1 vote


I was handling the ArrayList in java and learning through my mistakes. Here I come to know that can I use an ArrayList class method to replace an item at a specific location in an ArrayList? If it is possible, please let me know the way or method which I can use to do this.


12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
Best answer


Hello Gavin,

I hope you are doing fine. Yes you can make an ArrayList class method and manipulate a particular data from that list by calling a method  called.set()

You need to specify the location and the string inside the method of .set() This method will modify your data from the ArrayList at your specified location of the Array. Let’s have a look into this program below:


import java.util.ArrayList;
import java.util.List;

public class SetDemo {

  public static void main(String[] args) {

    List<String> top3Footballers = new ArrayList<String>();
    top3Footballers.add("Lionel Messi");
    top3Footballers.add("Christiano Ronaldo");
    top3Footballers.add("Neymar Jr.");
    System.out.println("ArrayList before replace: " + top3Footballers);

    top3Footballers.set(2, "Antoine Griezmann");

    System.out.println("ArrayList after replace: " + top3Footballers);


Here I declared an ArrayList and set the value under top3Footballers.

Later, I modified the value by using .set() method. This program will produce the output below:


ArrayList before replace: [Lionel Messi, Christiano Ronaldo, Neymar Jr.] 

ArrayList after replace: [Lionel Messi, Christiano Ronaldo, Antoine Griezmann]


I hope this helps you. Keep mistaking and learning. Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 107 views
Problem: Hello! I am working with ArrayLists in Java. I have declared an ArrayList and added elements in it but now I want to add another element at a specific index while moving the other elements one index ahead. I do not know how I can do that. What method can I use? Is there any method in ArrayList class that can be used for this purpose or do I have to write a custom method?
asked Nov 30, 2020 Code Learner 9.8k points
0 votes
1 answer 3 views
Problem: Please let me know any solution regarding this .. You can use index to traverse elements in an arraylist.
asked 22 hours ago Ifra 36.4k points
0 votes
1 answer 17 views
Problem: I am trying to write a java program to create an ArrayList, add all the months of a year, and print the months. I have successfully written a program by initializing a string array with names of the months and then adding the months in ArrayList by using a for a loop. But I want to know, is there any efficient way of doing this? A demo program would be much helpful. Thanks!
asked Feb 28 Code Learner 9.8k points
0 votes
1 answer 23 views
Problem: can you help me in dealing with this :How to return an ArrayList in java ?
asked Mar 5 Wafa Abu Yousef 6.1k points
0 votes
1 answer 34 views
Explain with an Example specially the syntax and program to illustrate its usage.
asked Nov 13, 2020 jagruthi 680 points
1 vote
1 answer 15 views
Problem : Getting error in sending arraylist of object from adapter to other activity. I wanna send my arraylist from onBindViewHolder method of Adapter to another activity but its is showing null exception error on the Cast Class in writetoParcel Method. Getting Error ... Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference How to send arraylist properly?
asked Mar 31 Sabbir Hossain 14.6k points
0 votes
1 answer 2 views
Problem: Which method can be used to return arraylist in java?
asked Apr 19 anika11 32.2k points
0 votes
1 answer 5 views
Problem: How does ArrayList work?What data structure does an ArrayList use internally?
asked Apr 12 Humaira ahmed 50.7k points
0 votes
1 answer 3 views
Question: I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?
asked Apr 20 munim01 21k points
0 votes
1 answer 5 views
Question: I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?
asked Apr 13 Atik03 26.5k points