2024-11-03 01:35:16
谢谢,只是这个只能生成临时变量并不能永久改变list里的对象吧?
新建一个list将小写的字符串全都保存起来就可以了
public class ToLowerCase {
public static void main(String[] args) {
List list = new ArrayList();
List newList = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
Iterator it = list.iterator();
while(it.hasNext()){
newList.add(String.valueOf(it.next()).toLowerCase());
}
Iterator newIt = newList.iterator();
while(newIt.hasNext()){
System.out.println(newIt.next());
}
}
}
另外,不能直接改变String的值,但是如果list里存储的是Object,是可以改变的,例如:
public class ToLowerCase {
public static void main(String[] args) {
List list = new ArrayList();
ToLowerCase tlc = new ToLowerCase();
list.add(tlc.new People("t1"));
list.add(tlc.new People("t2"));
list.add(tlc.new People("t3"));
Iterator it = list.iterator();
while(it.hasNext()){
People p = it.next();
p.setName("ttt");
}
Iterator it2 = list.iterator();
while(it2.hasNext()){
People p = it2.next();
System.out.println(p.name);
}
}
class People{
String name = null;
public People(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
2024-11-03 01:16:03
调用toLowerCase()方法的时候编译总是通不过啊,总是显示需要String类型,强制转换也不行
麻烦具体写下for循环里面的语句吧 谢谢啦