定义一个整型的有一百个元素的数组va[99],现在其中缺少一个元素,但是不知道那个,先找到这个元素然后再插入进去,利用递归算法解决~~~~~~~~~

解决方案 »

  1.   

    public class ArrayInsertTest {    public ArrayInsertTest() {        super();
        }    public int search(int[] ints, int aInt) {        if (ints == null) {
                throw new IllegalArgumentException("数组不能为空! ");
            }        int length = ints.length;
            for (int i = 0; i < length; i++) {
                if (ints[i] == aInt) {
                    return i;
                }
            }        return -1;
        }    public int[] insert(int[] ints, int aInt, int index) {        if (ints == null) {
                throw new IllegalArgumentException("数组不能为空! ");
            }        int length = ints.length + 1;
            int[] newInts = new int[length];        System.arraycopy(ints, 0, newInts, 0, index);
            newInts[index] = aInt;        System.arraycopy(ints, index, newInts, index + 1, length - index - 1);        return newInts;
        }    /**
         * @param args
         */
        public static void main(String[] args) {        int[] ints = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int[] shortIts = new int[] {2, 4, 6, 7, 8, 9};        ArrayInsertTest test = new ArrayInsertTest();        int length = ints.length;
            int[] newInts = shortIts;        for (int i = 0; i < length; i++) {
                if (test.search(newInts, ints[i]) < 0) {
                    newInts = test.insert(newInts, ints[i], i);
                }
            }        for (int i = 0; i < length; i++) {
                System.out.println("int[" + i + "]=" + newInts[i]);
            }
        }}