9910

          單飛

             :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
          // 用泛型實(shí)現(xiàn)通用數(shù)組的刪除操作
          package test;

          import java.lang.reflect.Array;
          import java.util.ArrayList;
          import java.util.List;

          public class Test1 {
               
          public static <T> T[] deleteArray(T[] items,int index) {
                  
          if (items == null || items.length == 0 || index < 0
                          
          || index > items.length - 1) {
                      
          return items;
                  }
                  T[] newItems 
          = (T[]) Array.newInstance(items.getClass().getComponentType(), items.length - 1);
                  
          if(index == 0){
                      
          // first
                      System.arraycopy(items, 1, newItems, 0, newItems.length);    
                  }
          else if(index == items.length - 1){
                      
          // end
                      System.arraycopy(items, 0, newItems, 0, newItems.length);
                  }
          else{
                      
          // middle
                      System.arraycopy(items, 0, newItems, 0, index);
                      System.arraycopy(items, index
          +1, newItems, index, items.length - index -1);            
                  }
                  
                  
          return newItems;
                  
              }
              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub
                  
          // first
                  String[] first = new String[]{"0","1","2"};
                  String[] rt 
          = deleteArray(first,0);
                  printArray(rt);
                  
          // end
                  Integer[] end = new Integer[]{0,1,2};
                  Integer[]  rt1 
          = deleteArray(end,2);
                  printIntegerArray(rt1);
              }
              
              
          public static void printArray(String[] array){
                  System.out.println(
          "start ====================");
                  
          for(String s:array){
                      System.out.println(s);
                  }
                  System.out.println(
          "end ====================");
                  
                  List
          <String> a = new ArrayList<String>();
                  
              }
              
              
          public static void printIntegerArray(Integer[] array){
                  System.out.println(
          "start ====================");
                  
          for(Integer s:array){
                      System.out.println(s);
                  }
                  System.out.println(
          "end ====================");
                  
                  List
          <String> a = new ArrayList<String>();
                  
              }

          }
          posted on 2008-09-23 21:44 單飛 閱讀(500) 評(píng)論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 比如县| 咸阳市| 肃北| 微博| 息烽县| 南靖县| 体育| 青海省| 湾仔区| 定结县| 鲁甸县| 大邑县| 弥勒县| 武义县| 辽中县| 靖州| 双牌县| 鄱阳县| 娱乐| 五指山市| 五原县| 腾冲县| 雷州市| 神农架林区| 南和县| 方城县| 盐池县| 余干县| 池州市| 肇庆市| 安西县| 浮梁县| 萨迦县| 马关县| 策勒县| 边坝县| 镇巴县| 玉门市| 崇阳县| 洛宁县| 肥东县|