• Register
1 vote
115 views

Problem:

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.

Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

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 43 views
43 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 6k points
0 votes
1 answer 9 views
9 views
Problem: I need some help for the private static ArrayList getAuthors(String authors) method. I am kind a beginner. so please help me finish this drive method. or give me some directions. Instruction some of the elements of the allAuthors array contain ... names. The getAuthors method uses this asterisk as a delimiter between names to store them separately in the returned ArrayList of Strings.
asked Feb 3 Ali 4.8k points
0 votes
1 answer 30 views
30 views
Explain with an Example specially the syntax and program to illustrate its usage.
asked Nov 13, 2020 jagruthi 680 points
0 votes
2 answers 166 views
166 views
Problem: You use this method to determine the number of items stored in an ArrayList object?
asked Apr 18, 2020 ArifulIslam 7.5k points
0 votes
1 answer 8 views
8 views
Solve this error.
asked Feb 11 TeamScript 13.5k points
0 votes
1 answer 8 views
8 views
Is there any method to do this?
asked Dec 25, 2020 TeamScript 13.5k points
0 votes
1 answer 5 views
5 views
Problem: In Java Programming, Can we call a static strategy for a theoretical class? Indeed I realize we can't utilize static with a technique for a theoretical class. in any case, I need to know why?
asked Feb 19 TeamScript 13.5k points
0 votes
1 answer 13 views
13 views
How would I do this? Thanks!
asked Feb 11 TeamScript 13.5k points