[Java] 数组工具类 (集合,Array的增、删、改、查等操作) →→→→→进入此内容的聊天室

来自 , 2020-12-06, 写在 Java, 查看 121 次.
URL http://www.code666.cn/view/8e82ab72
  1. package org.loon.framework.db.test.util;
  2. import java.io.Serializable;
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.Random;
  8. /** */
  9. /**
  10.  * <p>
  11.  * Title: LoonFramework
  12.  * </p>
  13.  * <p>
  14.  * Description:ArrayUtil,数组操作工具类 (实现集合类部分功能,用于Array的增、删、改、查等操作)
  15.  * </p>
  16.  * <p>
  17.  * Copyright: Copyright (c) 2007
  18.  * </p>
  19.  * <p>
  20.  * Company: LoonFramework
  21.  * </p>
  22.  *
  23.  * @author chenpeng
  24.  * @email:ceponline@yahoo.com.cn
  25.  * @version 0.1
  26.  */
  27. public class ArrayUtil implements Serializable {
  28.     /** */
  29.     /**
  30.      *
  31.      */
  32.     private static final long serialVersionUID = 8057374625909011982L;
  33.     // 缓存数组对象
  34.     private Object objArray;
  35.     // 数组长度
  36.     private int size = 0;
  37.     // 缓存数组对象实体类型
  38.     private String objType;
  39.     final static private Random rand = new Random();
  40.     private static ArrayUtil instance = null;
  41.     /** */
  42.     /**
  43.      * 直接注入Collection
  44.      *
  45.      * @param collection
  46.      * @return
  47.      */
  48.     public static ArrayUtil getInstance(Collection collection) {
  49.         return getInstance(collection.toArray());
  50.     }
  51.     /** */
  52.     /**
  53.      * 直接注入对象数组
  54.      *
  55.      * @param array
  56.      */
  57.     public static ArrayUtil getInstance(Object array) {
  58.         if (instance == null) {
  59.             instance = new ArrayUtil(array);
  60.         }
  61.         return instance;
  62.     }
  63.     /** */
  64.     /**
  65.      * 注入类产生指定大小对象数组
  66.      *
  67.      * @param clazz
  68.      * @param maxSize
  69.      */
  70.     public static ArrayUtil getInstance(Class clazz, int maxSize) {
  71.         if (instance == null) {
  72.             instance = new ArrayUtil(clazz, maxSize);
  73.         }
  74.         return instance;
  75.     }
  76.     private ArrayUtil() {
  77.     }
  78.     /** */
  79.     /**
  80.      * 注入对象数组产生指定大小对象数组
  81.      *
  82.      * @param clazz
  83.      * @param maxSize
  84.      */
  85.     private ArrayUtil(Class clazz, int maxSize) {
  86.         // 转为指定大小对象数组
  87.         Object array = (Object[]) Array.newInstance(clazz, maxSize);
  88.         // 初始化
  89.         init(array);
  90.     }
  91.     /** */
  92.     /**
  93.      * 直接注入对象数组
  94.      *
  95.      * @param array
  96.      */
  97.     private ArrayUtil(Object array) {
  98.         init(array);
  99.     }
  100.     private void init(Object array) {
  101.         // 检查是否数组对象
  102.         if (!(array instanceof Object[])) {
  103.             throw new IndexOutOfBoundsException("Not object arrays!");
  104.         }
  105.         // 缓存数组对象
  106.         objArray = array;
  107.         // 缓存实体类型
  108.         objType = array.getClass().getComponentType().getSimpleName();
  109.         // 缓存数组长度
  110.         size = Array.getLength(objArray);
  111.     }
  112.     /** */
  113.     /**
  114.      * 返回指定对象索引位置
  115.      *
  116.      * @param obj
  117.      * @return
  118.      */
  119.     public int get(Object obj) {
  120.         // 检查是否合法对象
  121.         checkObject(obj);
  122.         Object[] object = (Object[]) objArray;
  123.         for (int i = 0; i < size; i++)
  124.             if (object[i] == obj) {
  125.                 return i;
  126.             }
  127.         return -1;
  128.     }
  129.     /** */
  130.     /**
  131.      * 返回指定索引位置对象
  132.      *
  133.      * @param index
  134.      * @return
  135.      */
  136.     public Object get(int index) {
  137.         checkIndex(index);
  138.         return getObjectArray()[index];
  139.     }
  140.     /** */
  141.     /**
  142.      * 加载对象在指定位置
  143.      *
  144.      * @param obj
  145.      * @param index
  146.      */
  147.     public void add(Object obj, int index) {
  148.         // 检查索引是否越界
  149.         checkIndex(index);
  150.         // 检查是否合法对象
  151.         checkObject(obj);
  152.         Object[] objTemp = (Object[]) objArray;
  153.         objTemp[index] = obj;
  154.         // copy临时数组到objArray
  155.         System.arraycopy(objTemp, 0, objArray, 0, objTemp.length);
  156.     }
  157.     /** */
  158.     /**
  159.      * 加载对象
  160.      *
  161.      * @param obj
  162.      */
  163.     public void add(Object obj) {
  164.         // 类型检查
  165.         checkObject(obj);
  166.         // 累加
  167.         next();
  168.         // 临时缓存旧数组数组
  169.         Object[] objTemp = new Object[size];
  170.         // 加载对象
  171.         objTemp[size - 1] = obj;
  172.         // copy
  173.         System.arraycopy(objArray, 0, objTemp, 0, Array.getLength(objArray));
  174.         // 转换
  175.         objArray = objTemp;
  176.     }
  177.     /** */
  178.     /**
  179.      * 删除指定索引位置数组数据
  180.      *
  181.      * @param index
  182.      * @return
  183.      */
  184.     public Object remove(int index) {
  185.         // 检查索引是否越界
  186.         checkIndex(index);
  187.         Object[] objTemp = (Object[]) objArray;
  188.         // 重新构建objArray
  189.         int j;
  190.         if ((j = size - index - 1) > 0) {
  191.             System.arraycopy(objTemp, index + 1, objTemp, index, j);
  192.         }
  193.         // 减少size
  194.         back();
  195.         return objTemp[index];
  196.     }
  197.     public boolean contains(Object obj) {
  198.         Object[] objTemp = (Object[]) objArray;
  199.         for (int i = 0; i < size; i++) {
  200.             if (hash(objTemp[i]) == hash(obj)) {
  201.                 return true;
  202.             }
  203.         }
  204.         return false;
  205.     }
  206.     public Object[] sub(int startIndex, int endIndex) {
  207.         // 验证索引范围
  208.         checkIndex(startIndex);
  209.         checkIndex(endIndex);
  210.         int over = endIndex - startIndex;
  211.         if (over < 0) {
  212.             throw new IndexOutOfBoundsException(
  213.                 "Index beyond the end of the border!");
  214.         }
  215.         Object[] objTemp = (Object[]) objArray;
  216.         Object[] objs = (Object[]) Array.newInstance(objArray.getClass()
  217.                         .getComponentType(), over);
  218.         for (int i = startIndex; i < endIndex; i++) {
  219.             objs[i - 1] = objTemp[i - 1];
  220.         }
  221.         return objs;
  222.     }
  223.     public void clear() {
  224.         Object[] objTemp = (Object[]) objArray;
  225.         // 清空数据
  226.         for (int i = 0; i < size; i++) {
  227.             objTemp[i] = null;
  228.             size = 0;
  229.         }
  230.     }
  231.     /** */
  232.     /**
  233.      * 删除指定的对象实体
  234.      *
  235.      * @param obj
  236.      * @return
  237.      */
  238.     public boolean remove(Object obj) {
  239.         // 检查是否合法对象
  240.         checkObject(obj);
  241.         Object[] object = (Object[]) objArray;
  242.         for (int i = 0; i < size; i++)
  243.             if (object[i] == obj) {
  244.                 remove(i);
  245.                 return true;
  246.             }
  247.         return false;
  248.     }
  249.     /** */
  250.     /**
  251.      * 混淆数组元素
  252.      *
  253.      * @return
  254.      */
  255.     public void mixElements() {
  256.         mixElements(objArray);
  257.     }
  258.     /** */
  259.     /**
  260.      * 检查数组内元素是否为空
  261.      *
  262.      * @return
  263.      */
  264.     public boolean isEmpty() {
  265.         return (size == 0);
  266.     }
  267.     /** */
  268.     /**
  269.      * 转为list
  270.      *
  271.      * @return
  272.      */
  273.     public List getList() {
  274.         return Arrays.asList((Object[]) objArray);
  275.     }
  276.     /** */
  277.     /**
  278.      * 减少size
  279.      *
  280.      */
  281.     private void back() {
  282.         size--;
  283.     }
  284.     /** */
  285.     /**
  286.      * 增加size
  287.      *
  288.      */
  289.     private void next() {
  290.         size++;
  291.     }
  292.     /** */
  293.     /**
  294.      * 检查索引是否溢出
  295.      *
  296.      * @param index
  297.      */
  298.     private void checkIndex(int index) {
  299.         if (index >= size || index < 0) {
  300.             throw new IndexOutOfBoundsException("Index " + index
  301.                                                 + " out of bounds!");
  302.         }
  303.     }
  304.     /** */
  305.     /**
  306.      * 检查对象类型
  307.      *
  308.      * @param obj
  309.      */
  310.     private void checkObject(Object obj) {
  311.         if (obj instanceof Object[]) {
  312.             throw new IndexOutOfBoundsException("Not loading arrays!");
  313.         }
  314.         String type;
  315.         if (!objType.equals(type = obj.getClass().getSimpleName())) {
  316.             throw new IndexOutOfBoundsException("Not this " + type
  317.                                                 + " type of loading!");
  318.         }
  319.     }
  320.     /** */
  321.     /**
  322.      * 扩充数组对象
  323.      *
  324.      * @param obj
  325.      * @param i
  326.      * @param flag
  327.      * @return
  328.      */
  329.     static public Object expand(Object obj, int i, boolean flag) {
  330.         int j = Array.getLength(obj);
  331.         Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j
  332.                                         + i);
  333.         System.arraycopy(obj, 0, obj1, flag ? 0 : i, j);
  334.         return obj1;
  335.     }
  336.     /** */
  337.     /**
  338.      * 扩充数组对象
  339.      *
  340.      * @param obj
  341.      * @param i
  342.      * @param flag
  343.      * @return
  344.      */
  345.     static public Object expand(Object obj, int i) {
  346.         return expand(obj, i, true);
  347.     }
  348.     /** */
  349.     /**
  350.      * 随机返回数组内容
  351.      *
  352.      * @param obj
  353.      */
  354.     static public void mixElements(Object obj) {
  355.         int i = Array.getLength(obj);
  356.         for (int k = 0; k < i; k++) {
  357.             int j = getRandom(k, i - 1);
  358.             Object obj1 = Array.get(obj, j);
  359.             Array.set(obj, j, Array.get(obj, k));
  360.             Array.set(obj, k, obj1);
  361.         }
  362.     }
  363.     static public Random getRandomObject() {
  364.         return rand;
  365.     }
  366.     static public int getRandom(int i, int j) {
  367.         return i + rand.nextInt((j - i) + 1);
  368.     }
  369.     private int hash(Object obj) {
  370.         int h = obj.hashCode();
  371.         h += ~(h << 9);
  372.         h ^= (h >>> 14);
  373.         h += (h << 4);
  374.         h ^= (h >>> 10);
  375.         return h;
  376.     }
  377.     public int hashCode() {
  378.         return hash(objArray.getClass());
  379.     }
  380.     public int size() {
  381.         return size;
  382.     }
  383.     /** */
  384.     /**
  385.      * 反回当前数组对象
  386.      *
  387.      * @return
  388.      */
  389.     public Object[] getObjectArray() {
  390.         return (Object[]) objArray;
  391.     }
  392.     public static void main(String[] args) {
  393.         /*
  394.          * package org.loon.framework.db.test.util;
  395.          *
  396.          *
  397.          * //测试实体类
  398.          *
  399.          * public class TestBean {
  400.          *
  401.          * String name;
  402.          *
  403.          * int id;
  404.          *
  405.          * public int getId() { return id; }
  406.          *
  407.          * public void setId(int id) { this.id = id; }
  408.          *
  409.          * public String getName() { return name; }
  410.          *
  411.          * public void setName(String name) { this.name = name; }
  412.          *
  413.          * }
  414.          */
  415.         /**//*
  416.              * TestBean[] tb = new TestBean[3]; for (int i = 0; i < tb.length;
  417.              * i++) { tb[i] = new TestBean(); tb[i].setName("name" + i);
  418.              * tb[i].setId(i); } //直接载入已有数组对象 ArrayUtil arrayUtil =
  419.              * ArrayUtil.getInstance(tb); TestBean tb1 = new TestBean(); //
  420.              * arrayUtil.add(tb[0]); arrayUtil.remove(tb[0]); //
  421.              * arrayUtil.remove(tb[2]);
  422.              * System.out.println(arrayUtil.contains(tb1));
  423.              * System.out.println(((TestBean) arrayUtil.get(0)).getName());
  424.              * System.out.println(arrayUtil.size()); // 打乱数组
  425.              * arrayUtil.mixElements(); for (int i = 0; i < arrayUtil.size();
  426.              * i++) { System.out.println(((TestBean)
  427.              * arrayUtil.get(i)).getName()); }
  428.              */
  429.         /**
  430.          * // 生成TestBean的数组实例,初始容量为5 ArrayUtil arrayUtil =
  431.          * ArrayUtil.getInstance(TestBean.class, 5); TestBean t = new
  432.          * TestBean(); t.setName("test"); // 在数组载入t的实例 arrayUtil.add(t, 0);
  433.          * TestBean t1 = new TestBean(); t1.setName("test1"); arrayUtil.add(t1,
  434.          * 1); arrayUtil.add(t, 2); arrayUtil.add(t, 3); arrayUtil.add(t, 4); //
  435.          * 会自动增加数组容量 arrayUtil.add(t); // 显示索引5数据 System.out.println(((TestBean)
  436.          * arrayUtil.get(5)).getName()); // 截取索引1-3,显示1数据
  437.          * System.out.println(((TestBean) arrayUtil.sub(1, 3)[1]).getName());
  438.          */
  439.     }
  440. }

回复 "数组工具类 (集合,Array的增、删、改、查等操作)"

这儿你可以回复上面这条便签

captcha