不过如果换成了引用类型的就可以改变了,同样的你也可以把int类型的装箱成object类型的,当然,在修改值的时候需要重新拆箱再装箱,性能损失有一些!
下面的例子输出结果为:
21
23
25
代码:
using System
using System.Collections.Generic
using System.Linq
using System.Text
namespace ListDemo
{
public class Person
{
private string name
private int age
public string Name
{
get { return name}
set { name = value}
}
public int Age
{
get { return age}
set { age = value}
}
public Person() { }
public Person(int age) { this.age = age}
}
class Program
{
static void Main(string[] args)
{
List<Person>list = new List<Person>()
list.AddRange(new Person[] { new Person(11), new Person(13), new Person(15) })
list.ForEach(p =>p.Age += 10)
list.ForEach(
delegate(Person item)
{
Console.WriteLine(item.Age)
}
)
Console.ReadKey()
}
}
}
按照你的要求编写的Java程序如下
import java.util.Listimport java.util.ArrayList
public class A {
public static void main(String[] args) {
List<Integer> a=new ArrayList<Integer>()
for(int i=1i<=10i++){
a.add(i)
}
a.set(6,200)
System.out.println(a)
}
}
运行结果
[1, 2, 3, 4, 5, 6, 200, 8, 9, 10]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)