工作常用 随笔记录

  1、C#中数组的快速复制:  有4种方法

 1 //方法一:使用for循环
 2
 3 int []pins = {9,3,7,2}
 4 int []copy = new int[pins.length];
 5 for(int i =0;i!=copy.length;i++)
 6 {
 7   copy[i] = pins[i];
 8 }
 9
10
11//方法二:使用数组对象中的CopyTo方法
12 int []pins = {9,3,7,2}
13 int []copy2 = new int[pins.length];
14 pins.CopyTo(copy2,0);
15
16
17//方法三:使用Array类的一个静态方法Copy
18 int []pins = {9,3,7,2}
19 int []copy3 = new int[pins.length];
20 Array.Copy(pins,copy3,copy.Length);
21
22//方法四:使用Array类中的一个实例方法Clone(),可以一次调用,最方便,但是Clone()方法返回的是一个对象,所以要强制转换成恰当的类类型。
23 int []pins = {9,3,7,2}
24 int []copy4 = (int [])pins.Clone();

 

  2、C#中列表等容器的集合操作,与Java相对比

集合的操作即求两个集合之间的   交集   并集   差集三类。

代码类型 交集A∩B 并集 A∪B 差集A-B 另外一种差集A E B

C#中的函数: List<T>

HashSet<T>中也可以这样

A.Intersect(B) = {b}

var C = A.Intersect<T>(B);//并没有改变A集合
foreach (var item in C)
      Console.WriteLine(item.ToString());

C = A.Union<T>(B) = {a,b,c} C = A.Except<T>(B) = {a}  

C#中 另外一种HashSet<T>

A.IntersectWith(B); A = {c} A.UnionWith(B); A = {a,b,c} A.ExceptWith(B); A = {a} A.SymmetricExceptWith(B); A = {a,c}
Java中函数: List<T> A.retainAll(B) A.addAll(B) = {a,b,c} A.removeAll(B) = {a}  
集合 A∩B = {b} A∪B = {a,b,c} A-B = {a} A E B = {a,c}

 

 

 

 

 

 

  

 

  

  3、C#中的无符号按位右移的实现,实现了Java中的 >>> 运算符的功能

 

转载自:http://bbs.csdn.net/topics/80384607

 1 public static int foo(int x, int y)
 2  {
 3          int mask = 0x7fffffff; //int.MAX_VALUE
 4         for (int i = 0; i < y; i++)
 5      {
 6         x >>= 1;
 7         x &= mask;
 8       }
 9      return x;
10 }
11 
12 System.out.println((1 >>> 3) == foo(1, 3));
13 System.out.println((1 >>> 5) == foo(1, 5));
14 System.out.println((1 >>> 7) == foo(1, 7));
15 System.out.println((10 >>> 9) == foo(10, 9));
16 System.out.println((-1 >>> 5) == foo(-1, 5));
17 System.out.println((-9999 >>> 7) == foo(-9999, 7));
18 System.out.println((-1234 >>> 3) == foo(-1234, 3));
19 System.out.println((-10000 >>> 5) == foo(-10000, 5));
20 System.out.println((-10000 >>> 7) == foo(-10000, 7));

 

Java中 “>>>” 运算符是无符号的位移处理,无论是对有无符号的数据它会值的最高位视为无符号,所以作位移处理时,会直接在空出的高位填入0。 当我们要作位移的原始值并非代表数值时(例如:表示颜色图素的值,最高位并非正负号),可能就会需要使用此种无符号的位移。比如:

-10>>>2=1073741821

-10=1111 1111 1111 1111 1111 1111 1111 0110 (不管原来的“符号”位的值(一长串1),空上的全部直接填0)

0011 1111 1111 1111 1111 1111 1111 1101=1037341821

 

Java中其他的一些位操作:

~ 按位非(NOT)(一元运算)
& 按位与(AND)
| 按 位或(OR)
^ 按位异或(XOR)
>> 右移
>>> 右移,左边空出的位以0填 充
运算符 结果
<< 左移
&= 按位与赋值
|= 按位或赋值
^= 按 位异或赋值
>>= 右移赋值
>>>= 右移赋值,左边空出的位以0填充
<<= 左 移赋值

 

 

 

  4、C#中根据绝对路径获取路径中的文件名、目录、扩展名,根目录等操作

 1 string path = "C:\\dir1\\dir2\\foo.txt";
 2 string str = "GetFullPath:" + Path.GetFullPath(path) + "\r\n"; 
 3 str += "GetDirectoryName:" + Path.GetDirectoryName(path) + "\r\n";
 4 str += "GetFileName:" + Path.GetFileName(path) + "\r\n"; 
 5 str += "GetFileNameWithoutExtension:" + Path.GetFileNameWithoutExtension(path) + "\r\n";
 6 str += "GetExtension:" + Path.GetExtension(path) + "\r\n";
 7 str += "GetPathRoot:" + Path.GetPathRoot(path) + "\r\n"; MessageBox.Show(str);
 8 
 9 结果:
10 
11 GetFullPath:C:\dir1\dir2\foo.txt
12 
13 GetDirectoryName:C:\dir1\dir2
14 
15 GetFileName:foo.txt
16 
17 GetFileNameWithoutExtension:foo
18 
19 GetExtension:.txt
20 
21 GetPathRoot:C:\

 

   5、C#中根据绝对路径获取路径中的文件名、目录、扩展名,根目录等操作

 

 

 

 

 

 

 

 

 

 

 

 

 

AAAA

posted @ 2015-08-26 14:42  lee-yang  阅读(226)  评论(0编辑  收藏  举报