⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayutil.java

📁 struts spring ibatis
💻 JAVA
字号:
package com.struts2.framework.util;

import java.io.PrintStream;
import java.lang.reflect.Array;

public class ArrayUtil
{

    public static int[] append(int s1[], int a, boolean head)
    {
        return append(s1, new int[] {
            a
        }, head);
    }

    public static int[] append(int s1[], int s2[], boolean head)
    {
        if(s1 == null)
            return s2;
        if(s2 == null || s2.length == 0)
            return s1;
        int s3[] = new int[s1.length + s2.length];
        if(!head)
        {
            System.arraycopy(s1, 0, s3, 0, s1.length);
            System.arraycopy(s2, 0, s3, s1.length, s2.length);
        } else
        {
            System.arraycopy(s2, 0, s3, 0, s2.length);
            System.arraycopy(s1, 0, s3, s2.length, s1.length);
        }
        return s3;
    }

    public static long[] append(long s1[], long a, boolean head)
    {
        return append(s1, new long[] {
            a
        }, head);
    }

    public static long[] append(long s1[], long s2[], boolean head)
    {
        if(s1 == null)
            return s2;
        if(s2 == null || s2.length == 0)
            return s1;
        long s3[] = new long[s1.length + s2.length];
        if(!head)
        {
            System.arraycopy(s1, 0, s3, 0, s1.length);
            System.arraycopy(s2, 0, s3, s1.length, s2.length);
        } else
        {
            System.arraycopy(s2, 0, s3, 0, s2.length);
            System.arraycopy(s1, 0, s3, s2.length, s1.length);
        }
        return s3;
    }

    public static Object[] append(Object s1[], Object s2[], boolean head, Object type[])
    {
        Object objs[] = append(s1, s2, head);
        int size = objs.length;
        objs = (Object[])Array.newInstance(((Object) (type)).getClass().getComponentType(), size);
        return objs;
    }

    public static Object[] append(Object s1[], Object s2, boolean head)
    {
        return append(s1, new Object[] {
            s2
        }, head);
    }

    public static Object[] append(Object s1[], Object s2[], boolean head)
    {
        if(s1 == null)
            return s2;
        if(s2 == null)
            return s1;
        Object s3[] = (Object[])Array.newInstance(((Object) (s1)).getClass().getComponentType(), s1.length + s2.length);
        if(!head)
        {
            System.arraycopy(((Object) (s1)), 0, ((Object) (s3)), 0, s1.length);
            System.arraycopy(((Object) (s2)), 0, ((Object) (s3)), s1.length, s2.length);
        } else
        {
            System.arraycopy(((Object) (s2)), 0, ((Object) (s3)), 0, s2.length);
            System.arraycopy(((Object) (s1)), 0, ((Object) (s3)), s2.length, s1.length);
        }
        return s3;
    }

    public static boolean contain(int s1[], int a)
    {
        for(int i = 0; i < s1.length; i++)
            if(s1[i] == a)
                return true;

        return false;
    }

    public static boolean contain(long s1[], long a)
    {
        for(int i = 0; i < s1.length; i++)
            if(s1[i] == a)
                return true;

        return false;
    }

    public static Integer[] toObjectArray(int a[])
    {
        Integer arr[] = null;
        if(a != null)
        {
            arr = new Integer[a.length];
            for(int i = 0; i < a.length; i++)
                arr[i] = new Integer(a[i]);

        }
        return arr;
    }

    public static int[] removeItem(int s1[], int a)
    {
        if(s1 == null || s1.length == 0)
            return s1;
        int s2[] = new int[s1.length - 1];
        int i;
        for(i = 0; i < s2.length; i++)
        {
            if(s1[i] == a)
                if(i == s2.length)
                {
                    return s2;
                } else
                {
                    System.arraycopy(s1, i + 1, s2, i, s2.length - i);
                    return s2;
                }
            s2[i] = s1[i];
        }

        if(s1[i] == a)
            return s2;
        else
            return s1;
    }

    public static long[] removeItem(long s1[], long a)
    {
        if(s1 == null || s1.length == 0)
            return s1;
        long s2[] = new long[s1.length - 1];
        int i;
        for(i = 0; i < s2.length; i++)
        {
            if(s1[i] == a)
                if(i == s2.length)
                {
                    return s2;
                } else
                {
                    System.arraycopy(s1, i + 1, s2, i, s2.length - i);
                    return s2;
                }
            s2[i] = s1[i];
        }

        if(s1[i] == a)
            return s2;
        else
            return s1;
    }

    public static int[] removeIndex(int s1[], int index)
    {
        if(s1 == null || s1.length == 0)
            return s1;
        if(index < 0 || index >= s1.length)
            return s1;
        int s2[] = new int[s1.length - 1];
        if(index == 0)
            System.arraycopy(s1, 1, s2, 0, s2.length);
        else
        if(index == s2.length)
        {
            System.arraycopy(s1, 0, s2, 0, s2.length);
        } else
        {
            System.arraycopy(s1, 0, s2, 0, index);
            System.arraycopy(s1, index + 1, s2, index, s2.length - index);
        }
        return s2;
    }

    public static long[] removeIndex(long s1[], int index)
    {
        if(s1 == null || s1.length == 0)
            return s1;
        if(index < 0 || index >= s1.length)
            return s1;
        long s2[] = new long[s1.length - 1];
        if(index == 0)
            System.arraycopy(s1, 1, s2, 0, s2.length);
        else
        if(index == s2.length)
        {
            System.arraycopy(s1, 0, s2, 0, s2.length);
        } else
        {
            System.arraycopy(s1, 0, s2, 0, index);
            System.arraycopy(s1, index + 1, s2, index, s2.length - index);
        }
        return s2;
    }

    public static Object[] removeIndex(Object s1[], int index)
    {
        if(s1 == null || s1.length == 0)
            return s1;
        if(index < 0 || index >= s1.length)
            return s1;
        Object s2[] = (Object[])Array.newInstance(((Object) (s1)).getClass().getComponentType(), s1.length - 1);
        if(index == 0)
            System.arraycopy(((Object) (s1)), 1, ((Object) (s2)), 0, s2.length);
        else
        if(index == s2.length)
        {
            System.arraycopy(((Object) (s1)), 0, ((Object) (s2)), 0, s2.length);
        } else
        {
            System.arraycopy(((Object) (s1)), 0, ((Object) (s2)), 0, index);
            System.arraycopy(((Object) (s1)), index + 1, ((Object) (s2)), index, s2.length - index);
        }
        return s2;
    }

    public static Object[] removeItem(Object s1[], Object a)
    {
        if(s1 == null || s1.length == 0)
            return s1;
        Object s2[] = (Object[])Array.newInstance(((Object) (s1)).getClass().getComponentType(), s1.length - 1);
        int i;
        for(i = 0; i < s2.length; i++)
        {
            if(s1[i].equals(a))
                if(i == s2.length)
                {
                    return s2;
                } else
                {
                    System.arraycopy(((Object) (s1)), i + 1, ((Object) (s2)), i, s2.length - i);
                    return s2;
                }
            s2[i] = s1[i];
        }

        if(s1[i] == a)
            return s2;
        else
            return s1;
    }

    public static int[] moveItemUp(int s1[], int a)
    {
        for(int i = 0; i < s1.length; i++)
        {
            if(s1[i] != a)
                continue;
            if(i > 0)
            {
                s1[i] = s1[i - 1];
                s1[i - 1] = a;
            }
            break;
        }

        return s1;
    }

    public static int[] moveItemDown(int s1[], int a)
    {
        for(int i = 0; i < s1.length; i++)
        {
            if(s1[i] != a)
                continue;
            if(i + 1 < s1.length)
            {
                s1[i] = s1[i + 1];
                s1[i + 1] = a;
            }
            break;
        }

        return s1;
    }

    private static void debug(int s1[])
    {
        System.err.print("[");
        for(int i = 0; i < s1.length; i++)
            System.err.print(s1[i] + " ");

        System.err.println("]");
    }

    private static void debug(Object s1[])
    {
        System.err.print("[");
        for(int i = 0; i < s1.length; i++)
            System.err.print(s1[i] + " ");

        System.err.println("]");
    }

    public static void main(String args[])
    {
        int a[] = {
            2, 51, 445, 56, 6, 56, 5, 656, 5, 65, 
            65
        };
        int b[] = {
            2, 5, 7
        };
        Integer a1[] = {
            new Integer(1), new Integer(2), new Integer(3)
        };
        Integer b1[] = {
            new Integer(7), new Integer(5)
        };
        Integer c[] = (Integer[])append(a1, new Integer(8), true);
        debug(c);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -