zoukankan      html  css  js  c++  java
  • C# ArrayList InsertRange, LastIndexOf方法的学习

    LastIndexOf花了很长的时间才试出来是怎么回事,,,

    有点长了,,,

      1 using System;
      2 using System.Collections;
      3 
      4 namespace C_9_4
      5 {
      6     class Program
      7     {
      8         private  static void test_1(ArrayList b)
      9         {
     10             int i;
     11             if (b.IndexOf("SB") != -1)
     12             {
     13                 Console.WriteLine("找到了目标值,下标为{0}
    并输出数据检查是否正确", b.IndexOf("SB"));
     14 
     15                 for (i = 0; i < b.Count; i++)
     16                 {
     17                     Console.WriteLine("{0}", b[i]);
     18                 }
     19             }
     20             else
     21             {
     22                 Console.WriteLine("没有找到对应值");
     23             }
     24 
     25         }
     26         static void Main(string[] args)
     27         {
     28             //ArrayList.InsertRange
     29             //将某个集合插入到指定位置
     30             //格式ArrayList.InsertRange(Int32,Object);
     31             
     32             ArrayList a = new ArrayList() {1,2,3,4,5,6 };
     33             //为了简化程序,我们就直接保留了上次的test_1函数了
     34             test_1(a);
     35             ArrayList bbb = new ArrayList() {"S","SB","B","DSB" };
     36             a.InsertRange(0,bbb);
     37             test_1(a);
     38             //从这里我们可以看出来,InsertRange,和Insert原理差不多,都是直接插入,而且这里是顺序插入
     39             //那我们再来测试一下他是否和Insert一样,存在边界
     40 
     41             a = new ArrayList() { 1, 2, 3, 4, 5, 6 };
     42             a.InsertRange(5,bbb );
     43             test_1(a);
     44 
     45             a = new ArrayList() { 1, 2, 3, 4, 5, 6 };
     46             a.InsertRange(6, bbb);
     47             test_1(a);
     48 
     49             /*
     50             a = new ArrayList() { 1, 2, 3, 4, 5, 6 };
     51             a.InsertRange(7, bbb);
     52             test_1(a);
     53              
     54              */
     55             //从上面的式子我们可以看出来,InsertRange和Insert基本是完全一致的
     56 
     57             //ArrayList.LastIndexOf
     58             //返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引
     59             //说白了就是在ArrayList里面搜索某个元素,然后找到它最后一个元素,并返还它的下标
     60 
     61             //第一种格式
     62             //ArrayList1.LastIndexOf(Object);
     63             string SB = "SB";
     64             ArrayList c = new ArrayList() {1,9,2,3,"SB",'A',"SB",'5',7 };
     65             Console.WriteLine("我们要找的最后一个"{0}",它的下标为{1}", SB, c.LastIndexOf(SB));
     66             //直接在整个ArrayList里面索引Object,
     67 
     68             //第二种格式
     69             //ArrayList.LastIndexOf(Object,Int32);
     70             //Int32向后搜索的从零开始的起始索引。
     71             //LastIndexOf本质的不同之处是什么呢?
     72             //我们继续以c为例子
     73             int ii = 4;
     74             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32={1}," +
     75                 "它代表的是反向数的索引位数,那么正向索引应该是{2}," +
     76                 "Object的下标是{3}",SB ,ii,c.Count-ii-1,c.LastIndexOf (SB,ii));//4
     77             //任然不清楚到底是怎么索引的,我们来改变ii的值再来看一下
     78             ii =1;
     79             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32={1}," +
     80                 "它代表的是反向数的索引位数,那么正向索引应该是{2}," +
     81                 "Object的下标是{3}", SB, ii, c.Count - ii - 1, c.LastIndexOf(SB, ii));//-1
     82             //表示没有找到哦
     83             ii = 2;
     84             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32={1}," +
     85                 "它代表的是反向数的索引位数,那么正向索引应该是{2}," +
     86                 "Object的下标是{3}", SB, ii, c.Count - ii - 1, c.LastIndexOf(SB, ii));//-1
     87             //任然没找到
     88             ii = 3;
     89             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32={1}," +
     90                 "它代表的是反向数的索引位数,那么正向索引应该是{2}," +
     91                 "Object的下标是{3}", SB, ii, c.Count - ii - 1, c.LastIndexOf(SB, ii));//-1
     92             //还是没找到
     93             ii = c.Count-1;
     94             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32={1}," +
     95                 "它代表的是反向数的索引位数,那么正向索引应该是{2}," +
     96                 "Object的下标是{3}", SB, ii, c.Count - ii - 1, c.LastIndexOf(SB, ii));//6
     97             //终于找到了,这证明了,Int32其实是从前往后记数的,并不是从后往前数数的
     98             //总结推翻上面的,LastIndexOf的Int32任然是从前往后数的,区别在于他是
     99             //1 2 3 4 5 6 7 我索引到下标为5的,就是数字4
    100             //那就只看前面的1 2 3 4  后面的就不看了
    101 
    102             //第三种方法
    103             //ArrayList1.LastIndexOf(Object,Int32_1,Int32_2)
    104             //这就只讲Int32_1是向后搜索的从零开始的起始索引,Int32_2则是表示索引的个数
    105             //ArrayList c = new ArrayList() {1,9,2,3,"SB",'A',"SB",'5',7 };
    106             //这里我们依旧使用C来研究
    107             //请注意, LastIndexOf 是向后搜索; 因此, count 必须小于或等于 startIndex + 1。
    108 
    109             int iii ;
    110 
    111           
    112             iii = 5;
    113             Console.WriteLine ("我们要找到最后一个"{0}",我们设置的Int32_1={1}," +
    114                 "我们又设置Int32_2={2},它表示的是索引几个数据," +
    115                 "最后Object的下标是{3}",SB ,ii,iii,c.LastIndexOf(SB,ii,iii));//6
    116 
    117             iii = 2;
    118             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32_1={1}," +
    119                 "我们又设置Int32_2={2},它表示的是索引几个数据," +
    120                 "最后Object的下标是{3}", SB, ii, iii, c.LastIndexOf(SB, ii, iii));//-1
    121 
    122 
    123             iii = 4;
    124             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32_1={1}," +
    125                 "我们又设置Int32_2={2},它表示的是索引几个数据," +
    126                 "最后Object的下标是{3}", SB, ii, iii, c.LastIndexOf(SB, ii, iii));//6
    127 
    128             iii = 3;
    129             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32_1={1}," +
    130                 "我们又设置Int32_2={2},它表示的是索引几个数据," +
    131                 "最后Object的下标是{3}", SB, ii, iii, c.LastIndexOf(SB, ii, iii));//6
    132 
    133             ii = 5;
    134             iii = 3;
    135             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32_1={1}," +
    136                 "我们又设置Int32_2={2},它表示的是索引几个数据," +
    137                 "最后Object的下标是{3}", SB, ii, iii, c.LastIndexOf(SB, ii, iii));//4
    138 
    139             ii = 4;
    140             iii = 3;
    141             Console.WriteLine("我们要找到最后一个"{0}",我们设置的Int32_1={1}," +
    142                 "我们又设置Int32_2={2},它表示的是索引几个数据," +
    143                 "最后Object的下标是{3}", SB, ii, iii, c.LastIndexOf(SB, ii, iii));//4
    144             //得出结论
    145             //Int32_2表示的是,从Int32_1对应的值从右往左数
    146             //1 2 3 4 5 6 7 8 9我设置Int32_1为5,Int32_2为3
    147             //1 2 3 4 5 6 为止,只看这些元素
    148             //从右往左数
    149             //6 5 4数出3个
    150             //最终在4 5 6中搜索Object
    151 
    152 
    153 
    154         }
    155 
    156     }
    157 
    158 }
    悟已往之不谏,知来者之可追
  • 相关阅读:
    Memcached基本架构和思想
    varnish和squid的对比
    常用排序讲解
    数据结构堆的一种比较明白的讲解
    磁盘挂载MOUNT 445问题集
    mysql 如何提高批量导入的速度
    云平台涅槃重生计划
    NumPy、SciPy 等Python包在Windows下的whl安装包下载
    表迁移工具的选型-复制ibd的方法
    下一步的技术研究方向
  • 原文地址:https://www.cnblogs.com/ljh-study/p/13676177.html
Copyright © 2011-2022 走看看