• 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

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,240 points

Related questions

0 votes
1 answer 17 views
Explain with an Example specially the syntax and program to illustrate its usage.
asked Nov 13 jagruthi 680 points
0 votes
2 answers 69 views
Problem: You use this method to determine the number of items stored in an ArrayList object?
asked Apr 18 ArifulIslam 5.7k points
0 votes
1 answer 423 views
Problem : I am getting bellow error while compiling Arraylist exception in thread "main" java.lang.indexoutofboundsexception: index: 0, size: 0
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
1 answer 20 views
Problem: I am making a program which consists of solving a 2x2 equation, for this I used the Cramer method, the problem is not how to solve it, but it throws an error when executing it, and I suspect that it is because I used methods of the Matrix class Inside the Vector class ... " using namespace std; int main () { cout << "As long as the program compiles well it is enough" << endl; return 0; }
asked Nov 5 sasha 2.2k points