Java实例-List元素替换
以下实例演⽰了如何使⽤ Collections 类的 replaceAll() 来替换List中所有的指定元素:
Main.java ⽂件
import
java
.
util
.*;
public
class
Main
{
public
static
replaceall()void
main
(
String
[
]
args
)
{
List
list
=
Arrays
.
asList
(
"
one Two three Four five six one three Four
"
.
split
(
"
"
)
)
;
System
.
out
.
println
(
"
List :
"
+
list
)
;
Collections
.
replaceAll
(
list
,
"
one
"
,
"
hundrea
"
)
;
System
.
out
.
println
(
"
replaceAll:
"
+
list
)
;
}
}
以上代码运⾏输出结果为:
List:[one,Two, three,Four, five, six, one, three,Four]
replaceAll:[hundrea,Two, three,Four, five, six, hundrea, three,Four]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。