2.3装箱和取消装箱

本文探讨了使用 C# 结构体处理二维坐标数据的方法,并对比了 ArrayList 和 数组两种不同的数据结构在操作上的区别。通过具体实例展示了不同遍历方式对数据的影响。

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Collections;
 5 
 6 
 7     struct APoint
 8     {
 9         public int x;
10         public int y;
11         public APoint( int point_x,int point_y)
12 
13         { 
14             this.x=point_x;
15             this.y =point_y;
16 
17         }
18         public void Move(int offset_x, int offset_y)
19         {
20             x += offset_x;
21             y += offset_y;
22 
23         }
24         public override string ToString()
25         {
26             return x.ToString() + "," + y.ToString();
27         }//如果没有这行 结果显示 the0的坐标是(APoint)
28        
29         static void Main(string[] args)
30         {
31             Console.WriteLine("use ArrayList");
32             ArrayList pnts = new ArrayList(3);
33             for (int i = 0; i < 3; i++)
34             {
35                 pnts.Add(new APoint(100100));
36             }
37             for (int i = 0; i < 3; i++)
38            {
39                 ((APoint)pnts[i]).Move(400,400);
40             }
41             for (int i = 0; i < 3; i++)
42             {
43                 Console.WriteLine("the"+i.ToString()+"点坐标是("+pnts[i].ToString()+")");
44 
45             }
46             Console.WriteLine("使用数组");
47             APoint[] points = new APoint[3];
48             for (int i = 0; i < 3; i++)
49             {
50                 points[i] = new APoint(100,100);
51             }
52             Console.WriteLine("使用foreach循环");
53             foreach (APoint point in points)
54             {
55                 point.Move(400,400);
56             }
57             for (int i = 0; i < 3; i++)
58             {
59                 Console.WriteLine("" + i.ToString() + "点坐标是(" + pnts[i].ToString() + ")");
60             }
61             Console.WriteLine("使用for循环");
62             for (int i = 0; i < 3; i++)
63             {
64                 points[i].Move(400400);
65             }
66             for (int i = 0; i < 3; i++)
67             {
68                 Console.WriteLine(""+i.ToString()+"点坐标是("+points[i].ToString()+")");
69             }
70             Console.ReadLine();
71 

 

结果显示:

use ArrayList

the0点的坐标是(100,100)

the1点的坐标是(100,100)

the2点的坐标是(100,100)

使用数组

使用Foreach

第0点的坐标是(100,100)

第1点的坐标是(100,100)

第2点的坐标是(100,100)

使用For循环

第0点的坐标是(500,500)

第1点的坐标是(500,500)

第2点的坐标是(500,500)


转载于:https://www.cnblogs.com/zxlin25/archive/2009/02/15/1391045.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值