using System;
class a
{
public int[] data;
public a()
{
data=new int[3];
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=temp;
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
for(int i=0;i<3;i++)
Console.WriteLine(stack[0].data[i]);//输出为3,2,1 怎样让输出为1,2,3
Console.ReadLine();
}
}
解决方案 »
- c#找茬游戏鼠标的镜像显示问题
- 关于动态链接库的问题
- detailsView问题(请高手指教)
- [分享第1期]仿QQ托盘图标的左右键菜单(顶有分)
- 谁有together for vs.net 2005的crack
- 50分求助,Browser+ASP.net应用程序+后端数据库结构中,如何把绘图功能放在浏览器端?
- 关于用datatable更新源数据库的问题!
- 一个很简单的问题,解决立马给分!
- 有关安装程序得制作问题啊--Checkbox的值是什么啊
- 真是急死我也!在6~7两张碟里找不到visual sourcesafe安装程式,哪位高手指点一下迷津!谢谢!!!
- 学习使用Remoting,在途中遇到点问题
- 浮动广告 求助(帮忙改下)
{
Console.WriteLine(stack[0].data[i]);
}
Stack<int> stack =new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
..................
foreach(int i in stack)
{
Console.WriteLine(i)...
}
for(int i=temp.data.Length-1;i>-1;i--)
Console.WriteLine(stack[0].data[i]); //输出为1,2,3
Console.ReadLine();
}楼上的都有错,不应该是3,写死的话是i=2;i>-1;i--
{
public int[] data;
public a()
{
data=new int[3];
} public void Reverse()
{
Array.Reverse(data);
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=temp;
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
stack[0].Reverse();
for(int i=0;i<3;i++)
Console.WriteLine(stack[0].data[i]);//输出为3,2,1 怎样让输出为1,2,3
Console.ReadLine();
}
}
现在就是说要在stack数组中保存 temp对象
现在保存的是对象的引用 当修改temp.data[i]时候
stack里面的stack[0].data[i]也跟着变了
怎样将temp对象值拷贝一份到stack[0]对象中去 就这个意思
{
public int[] data;
public a()
{
data=new int[3];
} public int[] Reverse()
{
int[] temp =new int[data.Length];
data.CopyTo(temp,0);
Array.Reverse(temp);
return temp;
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=temp;
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
int[] arr=stack[0].Reverse();
for(int i=0;i<3;i++)
Console.WriteLine(arr[i]);//输出为3,2,1 怎样让输出为1,2,3
Console.ReadLine();
}
}
for (int i = 2; i>=0; i--)
Console.WriteLine(stack[0].data[i]);//输出为3,2,1 怎样让输出为1,2,3
class a
{
public int[] data;
public a()
{
data=new int[3];
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
//a.data.CopyTo(stack,0,3); //另一种方式,按照1,2,3次序赋值给stack for(int i=0;i<3;i++)
stack[i]=a.data[i]; //按照1,2,3次序赋值给stack
//stack[i]=a.data[3-i]; //按照3,2,1次序赋值给stack
}
}
象楼上的还在回复
C#中有两种类型变量,一种是值类型变量,一种是引用类型变量。对于前者,copy是属于全盘复制;而对于后者,一般的copy只是浅copy,相当于只传递一个引用指针一样。因此对于后者进行真正copy的时候,也是最费事的,具体的说,必须为其实现ICloneable接口中提供的Clone方法。浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用.
深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的public class Test
{
public class Person : ICloneable
{
public int ID;
public int age;
public void Show()
{
Console.WriteLine("ID:{0} 年龄:{1}",ID, age);
}
public object Clone()
{
Person newPerson = new Person();
newPerson.ID = this.ID;
newPerson.age = this.age;
return newPerson;
}
}
public static void ShowPersons(Person[] persons)
{
for (int i=0; i<persons.GetLength(0); i++)
{
persons[i].Show();
}
}
static void Main(string[] argv)
{
Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
Person[] persons = new Person[4];
for (int i=0;i<persons.GetLength(0);i++)
{
persons[i] = new Person();
persons[i].ID = rnd.Next()%10;
persons[i].age = rnd.Next()%50;
}
//打印原始数组
Console.WriteLine("打印原始数组");
ShowPersons(persons);
//深拷贝
Person[] personsCopied = new Person[4];
for (int i=0;i<personsCopied.GetLength(0);i++)
{
personsCopied[i] = (Person)persons[i].Clone();
}
//persons.CopyTo(personsCopied, 0);
Console.WriteLine("深拷贝的数组");
ShowPersons(personsCopied);
// //浅拷贝
// Person[] personsCloned = (Person[])persons.Clone();
// Console.WriteLine("浅拷贝的数组");
// ShowPersons(personsCloned);
//对原数组做修改
persons[2].ID += 10;
Console.WriteLine("打印修改后的原始数组");
ShowPersons(persons);
Console.WriteLine("深拷贝的数组应该不改变");
ShowPersons(personsCopied);
// Console.WriteLine("浅拷贝的数组应该改变");
// ShowPersons(personsCloned);
}
}
class a
{
public int[] data;
public a()
{
data=new int[3];
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0] = new a();
temp.data.CopyTo(stack[0].data,0,3);
}
class a
{
public int[] data;
public a()
{
data=new int[3];
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=temp;
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
for(int i=2;i>=0;i--)
Console.WriteLine(stack[0].data[i]);//输出为1,2,3
Console.ReadLine();
}
}
正规的做法,如果你是想让类可以克隆,那就推荐实现克隆接口。如果只是简单的类,没有太多的属性,也可以采取属性赋值的方法。如果要克隆的类是无法修改的,不受你控制的,例如系统类,其他人写的类,可以使用反射获取属性赋值。如果克隆的情形有时候并非完整的,即有条件策略的,或者此类不想影响子类,强制子类也实现克隆接口,则可以采取设计模式中的Prototype(原型),在专属的一个服务类中处理此事。无论如何做,都是按照需求来的,看你自己的需求来决定。但对于引用类型和值类型的区别,内存管理方式,一定要深入掌握。另外,建议您下次提问时,一开始就说清楚你的真实意图。看这个贴中多少人被你的提问误导了……
class a
{
public int[] data;
public a()
{
data = new int[3];
}
public a(a olda)
{
data=new int[3];
Array.Copy(olda.data,data,data.Length);
} a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=new a(temp);
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
for(int i=0;i<3;i++)
Console.WriteLine(stack[0].data[i]);//输出为1,2,3
Console.ReadLine();
引用 1 楼 jijunwu 的回复:
for(int i=3;i>0;i--)
{
Console.WriteLine(stack[0].data[i]);
}
for(int i=3;i>0;i--)
{
......
}
{
Console.WriteLine(stack[0].data[i]);
}
class a
{
public int[] data;
public a()
{
data=new int[3];
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=temp;
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
for(int i=0;i<3;i++)
{
for(int j=+1;i<3;i++)
{
if(stack[i]>stack[j])
{
int temp2=stack[i];
stack[i]=stack[j];
stack[j]=temp2;
}
}
// Console.WriteLine(stack[0].data[i]);//输出为3,2,1 怎样让输出为1,2,3
// Console.ReadLine();
}
for(int n=0;n<3;n++)
{
Console.WriteLine(stack[n]);
}
Console.ReadLine();
}
}
class a
{
public int[] data;
public a()
{
data=new int[3];
}
}
class example
{
static void Main()
{
a[] stack=new a[100];
a temp=new a();
temp.data[0]=1;
temp.data[1]=2;
temp.data[2]=3;
stack[0]=temp;
temp=new a();
temp.data[0]=3;
temp.data[1]=2;
temp.data[2]=1;
for(int i=0;i<3;i++)
Console.WriteLine(stack[0].data[i]);//输出为3,2,1 怎样让输出为1,2,3
Console.ReadLine();
}
}