利用泛型搜索出不同数据类型所在位置.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace XiaoTuNi.test.Study
{
    
public class test_FindElement
    
{

        
private static test_FindElement _test_FindElement;

        
public static test_FindElement GetTest_FindElement
        
{
            
get
            
{
                
if (_test_FindElement == null)
                
{
                    _test_FindElement 
= new test_FindElement();
                }

                
return _test_FindElement;
            }

        }


        
/// <summary>
        
/// 
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="searchArray"></param>
        
/// <param name="searchValue"></param>
        
/// <returns></returns>

        private int findElement<T>(T[] searchArray, T searchValue) where T : IComparable
        
{
            
int maxCount = searchArray.Length ;

            
if (maxCount > 0)
            
{
                
for (int i = 0; i < maxCount; i++)
                
{
                    
if (searchArray[i].CompareTo(searchValue) == 0)
                    
{
                        
return i;
                    }
//End if;
                }
//End for;
            }
//End if;

            
return -1;
        }


        
/// <summary>
        
/// 
        
/// </summary>
        
/// <returns></returns>

        public string CallGenericProcedure()
        
{
            
string[] stringArray = "廖海兵""白杰""小样""大样" };

            
string stringSearch = "小样";

            
int[] integerArray = 123456789011211314151617 };

            
int integerSearch = 11;

            
int mPosition = 0;

            StringBuilder sb 
= new StringBuilder();

            mPosition 
= findElement<string>(stringArray, stringSearch);//开始查找

            
if (mPosition < 0)
            
{
                sb.AppendLine(
"找不到字符串" + stringSearch);
            }

            
else
            
{
                sb.AppendLine(
"在位置" + mPosition.ToString() + "处找到" + stringSearch);
            }
//End if;

            mPosition 
= findElement<int>(integerArray, integerSearch);//开始查找

            
if (mPosition < 0)
            
{
                sb.AppendLine(
"找不到字符串" + integerSearch);
            }

            
else
            
{
                sb.AppendLine(
"在位置" + mPosition.ToString() + "处找到" + integerSearch);
            }
//End if;

            
return sb.ToString();
        }


    }

}

 

posted on 2008-01-19 16:57  小土泥  阅读(150)  评论(0编辑  收藏  举报

导航