zoukankan      html  css  js  c++  java
  • NET基础学习笔记6

    一,垃圾回收

    垃圾回收的目的:提高内存的利用率。

    垃圾回收器,只回收托管堆中的内存资源,不回收其他资源(数据库连接,文件话柄,网络端口等)。

    什么样的对象才会被回收:没有变量引用的对象。没有变量引用的对象,表示可以被回收了(null)

    什么时候回收:不确定,当程序需要新的内存的时候开始执行回收。GC.Collect();//手动调用垃圾回收器。不建议使用,垃圾回收时会暂停一下,让程序自动去GC。

    垃圾回收器中“代”的概念:垃圾回收采用了代的概念,避免了每次垃圾回收都遍历所有的对象,减少了垃圾回收的时间。第0代的回收频率高,第一代次之,第二代再次之。最后造成时间越久,存活率越高。
    共3代:第0代、第1代、第2代。
    各代的回收频率:第0代最高,其次第1代,再次第2代。也就是说越老的对象生存几率越大。
    .net中垃圾回收机制:mark-and-compact(标记和压缩)
    除了内存资源外的其他资源怎么办?~或者Dispose()

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _1垃圾回收
    {
        class Program:IDisposable
        {
            static void Main(string[] args)
            {
                
            }
    
            //在C++叫做析构函数。
            //在C#中叫Finalize()函数。中文名叫终结函数。
            //当当前对象在被垃圾回收前调用Finalize()函数,释放其他资源。 
            ~Program()
            {
    
            }
    
            public void Dispose()
            {
                //这里的代码。来释放内存以外的其他资源。
            }
    
            //总结:如果已经自己手动写Dispose()来释放资源了。就不需要调用~Program()来释放资源了。
        }
    }
    Dispose

    弱引用:WeakReference,对于创建比较耗时的对象可以使用若引用。

     1 #region 若应用
     2             Person p1 = new Person(); //强引用。垃圾回收不可以回收该对象。
     3             p1.name = "WeakReference";
     4             WeakReference wrf = new WeakReference(p1);
     5             p1 = null;//当执行完这句话,1.垃圾回收可以回收p1,2.通过弱引用还可以访问到p1.
     6             ////如果强制回收。就没有了
     7             //GC.Collect();
     8             object newp1 = wrf.Target;
     9             
    10             if (newp1 == null)
    11             {
    12                 Console.WriteLine("已经被回收");
    13             }
    14             else
    15             {
    16                 Console.WriteLine(((Person)newp1).name);
    17             }
    18 
    19             Console.ReadKey();
    20             
    21 
    22             #endregion
    弱引用例子

    二,集合的学习:

    1.ArrayList学习:

    Count。属性:实际个数。

    Capacity。属性:表示当前集合的容量。容量是每次扩展成倍添加的。

    Add()向集合增加元素。

    Remove()指定移除那个元素。

    RemoveAt(i)指定移除那个索引号的元素。

    TrimToSize()将总容量“收缩”为实际容量。

    Clear()清楚数组列表里面的元素。

    Contains()判断是否包含这个元素。

    ToArray()转化为obj数组。

    Sort()排序 Reverse()反转。

     1 ArrayList arrayList = new ArrayList();
     2             arrayList.Add("1");
     3             arrayList.Add(2);
     4             arrayList.Add("3");
     5             arrayList.Add(4);
     6             arrayList.Add("5");
     7             arrayList.Add(6);
     8             bool obool=arrayList.Contains(6);
     9             Console.WriteLine(obool);
    10             arrayList.Sort(0, 4, comparer);
    11             object[] objlist= arrayList.ToArray();
    12             foreach (var obj in objlist)
    13             {
    14                 Console.WriteLine(obj);
    15             }
    16             //int count = arrayList.Count;
    17 
    18             //for (int i = 0; i < 3; i++)
    19             //{
    20             //    arrayList.RemoveAt(i);
    21             //}
    22             //foreach (var item in arrayList)
    23             //{
    24             //    Console.WriteLine(item);
    25             //}
    26             //arrayList.TrimToSize();//把总用量收缩为实际数量。
    27             //Console.WriteLine(arrayList.Count);  //表示数组列表的个数。
    28             //Console.WriteLine(arrayList.Capacity); //当前集合用量。它的用量是每次翻倍扩展的。
    29             Console.ReadKey();
    ArrayList自己写的测试代码

    Hashtable 键值对:
    Add(键,值);添加

    ContainsKey(键);根据键判断是否存在。

    Remode()删除。

     1             Hashtable hashtalbe = new Hashtable();
     2             Person p=new Person{Name="haoren"};
     3             hashtalbe.Add(p.Name, p);
     4             Person p2 = new Person { Name = "xiaoming" };
     5             hashtalbe.Add(p2.Name, p2);
     6             bool iscontainskey=hashtalbe.ContainsKey("haoren");
     7             //hashtalbe.Remove(p2.Name);
     8             Console.WriteLine(iscontainskey);
     9             Console.WriteLine(((Person)hashtalbe[p2.Name]).Name);
    10             Console.ReadKey();          
    Hashtable

    三个小练习题:

    写随机数千万不要写在循环里面:通过random产生的随机数都是伪随机数,伪随机数需要一个种子。

     1 #region 第一题:两个(ArrayList)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。
     2             ////1.两个(ArrayList)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。
     3             //ArrayList arraylist1 = new ArrayList { "a", "b", "c", "d", "e" };
     4             //ArrayList arraylist2 = new ArrayList { "d", "e", "f", "g", "h" };
     5             //foreach (var item in arraylist1)
     6             //{
     7             //    if (!arraylist2.Contains(item))
     8             //    {
     9             //        arraylist2.Add(item);
    10             //    }
    11             //}
    12             //foreach (var item2 in arraylist2)
    13             //{
    14             //    Console.WriteLine(item2);
    15             //}
    16             
    17             #endregion
    18 
    19             #region 随机生成10个1-100之间的数放到ArrayList中,要求这10个数不能重复,并且都是偶数(添加10次,可能循环很多次。)
    20             //Random random = new Random();
    21             //ArrayList arrayList = new ArrayList();
    22             //int number=0;
    23             //for (int i = 0; i < 10; i++)
    24             //{
    25             //    bool IsEven = true;
    26             //    while (IsEven)
    27             //    {
    28             //        number = random.Next(0, 101);
    29             //        if (number % 2 == 0)
    30             //        {
    31             //            if (!arrayList.Contains(number))
    32             //            {
    33             //                arrayList.Add(number);
    34             //                IsEven = false;
    35             //            }
    36             //        }  
    37             //    }
    38             //}
    39 
    40             //foreach (var array in arrayList)
    41             //{
    42             //    Console.WriteLine(array);
    43             //}
    44             #endregion
    45 
    46             #region 有一个字符串是用空格分隔的一系列整数,写一个程序把其中的整数做如下重新排列打印出来:奇数显示在左侧、偶数显示在右侧。比如”2 7 8 3 22 9 5 11”显示成”7 3 9 2 8 22….”
    47             string str1 = "2 7 8 3 22 9 5 11";
    48             string[] stringlist=str1.Split(' ');
    49             StringBuilder oddlist = new StringBuilder();
    50             StringBuilder evenlist = new StringBuilder();
    51             foreach (var str in stringlist)
    52             {
    53                 if (int.Parse(str) % 2 == 0)
    54                 {
    55                     evenlist.Append(str+" ");
    56                 }
    57                 else
    58                 {
    59                     oddlist.Append(str + " ");
    60                 }
    61             }
    62             string newstr=oddlist.ToString() + evenlist.ToString();
    63             Console.WriteLine(newstr    );
    64             #endregion
    View Code

     2.List<T>

    命名空间System.Collections.Generic

    List<T>类似于ArrayList,ArrayList的升级版。带有各种方法:Sort(),Max(),Min(),Sum()。。。

    Dictionary<K,V>类类似于Hashtable,Hashtable的升级版。

    推介使用泛型集合。

    T,K,V就像一把锁,锁住集合只能存某种特定的类型,这里的T,K,V也可以使其他字母。

    泛型集合可以进行foreach遍历,是因为实现了IEnumerable<T>具有了GetEnumerator()方法。

    var是一个类型推断。

  • 相关阅读:
    leetcode 70 Climbing Stairs ----- java
    leetcode 69 Sqrt(x) ---java
    leetcode 68 Text Justification ----- java
    如何把iOS代码编译为Android应用
    OpenSource.com 评出 2014 年十佳开源软件
    js singleton
    Java NIO
    WPAD 的原理及实现
    WebKit JavaScript Binding添加新DOM对象的三种方式
    react
  • 原文地址:https://www.cnblogs.com/huijie/p/3204058.html
Copyright © 2011-2022 走看看