私有问题列表上的java迭代
我有一个SomeClass
类,里面有一个List<String> someList
。我需要让其他类在列表元素上迭代,但不允许它们更改列表或列表元素。测试以下代码:
public class SomeClass {
static private List<String> someList;
public static List<String> getSomeList() {
return someList;
}
SomeClass(Array<String> someArray){
someList=Collections.unmodifiableList(Arrays.asList(someArray));
}
}
public class DoSomething {
for (String s : SomeClass.getSomeList()){
s+="Gotcha";
}
}
将显示s只会在for循环中更改-一旦完成,getSomeList()将返回良好的旧列表,因此不会保存任何更改。正在尝试调用getSomeList()。add(“someString”)将导致异常:UnsupportedOperationException。 请(向经验丰富的C程序员)解释这种行为背后的逻辑,以及我如何捕获该异常
# 1 楼答案
来自javadoc, List stooges=数组。asList(“Larry”、“Moe”、“Curly”),它将返回一个由指定数组支持的固定大小的列表。 因此,对该列表的任何修改都将产生java。lang.UnsupportedOperationException即使代码中没有以下内容 收藏。不可修改列表(Arrays.asList(someArray))
# 2 楼答案
String的
+=
操作符实际上创建了一个新的字符串实例,而不是修改原始字符串对象。换句话说,以下两行代码是等效的:示例
下面是一个类似于
doSomeThing
的例子它的输出如下所示,这表明由
hello
引用的对象不受由helloReference
执行的+=
运算符的影响:在另一个世界中,在+=运算符之前:
在+=运算符之后,它将创建一个新实例,并修改
helloReference
引用的对象:因此,代码可以安全运行,doSomeThing中执行的操作将影响SomeClass引用的对象:
# 3 楼答案
因为,^{} 是immutable,所以更改不会反映在最终列表中。。使用^{} 或^{} 代替
String
作为列表的元素因为,^{} 正在返回^{} ,它是
Arrays
类中定义的嵌套类,如下所示:这个类扩展了^{} ,它反过来又扩展了^{} 。嵌套类} 方法。因此,当您在这个} 方法,该方法的定义如下:
ArrayList
没有重写AbstractList
的^{List
上调用add()
时,它会调用AbstractCollection
的^{这就是为什么你在打电话给}
add("something")
时会得到^{