Java ArrayList set() Method · Private Archive
Java ArrayList set() Method
The set() method is used to replace the element at the specified index in the dynamic array.
arraylist.set(int index, E element)
**Note:** arraylist is an object of the ArrayList class.
Parameter Description:
- The syntax of the set() method is:
- index - the index position
Return Value
element - the new element to be replaced at the index position
Returns the element previously at the index position.
Example
If the index value is out of range, an IndexOutOfBoundsException is thrown.
Example
import
java.util.ArrayList
;
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create a dynamic array
ArrayList
<
String
sites
=
new
ArrayList
<>
(
)
;
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
System
.
out
.
println
(
"Website list: "
sites
)
;
Replacing an element in the dynamic array:
String
element
=
sites.
set
(
2
,
"Wiki"
)
;
System
.
out
.
println
(
// The element at index 2 is replaced
sites
)
;
System
.
out
.
println
(
"After replacement: "
element
)
;
}
}
The output of the above program is:
替换后: [Google, Runoob, Wiki]
被替换的元素: Taobao
"Replaced element: "
In the example above, we created an array named sites; the set() method replaced Taobao at index position 2 with Wiki.
Note: If you are unsure of the element's index value, you can use the ArrayList indexOf() method.
ArrayList set() vs add() Method
// add() syntax
arraylist.add(int index, E element)
// set() syntax
arraylist.set(int index, E element)
The syntax of the add() and set() methods looks very similar.
Both methods add new elements to the array.
- However, there is a big difference between them:
- The add() method inserts an element into the dynamic array at the specified position.
Example
import
java.util.ArrayList
;
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
// Create a dynamic array
ArrayList
<
String
sites
=
new
ArrayList
<>
(
)
;
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
System
.
out
.
println
(
"Website list: "
sites
)
;
The set() method updates the element at the specified position.
ArrayList
<
String
sites2
=
new
ArrayList
<>
(
)
;
// Create another sites2 dynamic array
sites2.
addAll
(
sites
)
;
System
.
out
.
println
(
"ArrayList: "
sites
)
;
// Add the array elements from sites to sites2
sites.
set
(
1
,
"Wiki"
)
;
System
.
out
.
println
(
// Use set()
sites
)
;
"ArrayList after using set(): "
sites2.
add
(
1
,
"Wiki"
)
;
System
.
out
.
println
(
// Use add()
sites2
)
;
}
}
The output of the above program is:
网站列表: [Google, Runoob, Taobao]
ArrayList: [Google, Runoob, Taobao]
ArrayList 使用 set() 后: [Google, Wiki, Taobao]
ArrayList 使用 add() 后: [Google, Wiki, Runoob, Taobao]
"ArrayList after using add(): "
In the example above, we created two dynamic arrays named sites and sites2. We used the ArrayList addAll() method so that the two dynamic arrays have the same elements.
The set() method replaces the element at index position 1
The add() method adds the Wiki element at index position 1, changing the index of the Runoob element to 2. Java ArrayList add().
Java ArrayList