java用递归法在数组中查找,插入

先来看原题要求

package integerSet;

/**
 * This class provides methods to interact with a set of integers
 * implemented using a normal array of integers.
 * You <b>should</b> at least modify the
 * methods <code>member</code> (is an integer in the set) 
 * and <code>insert</code> (add a new integer to the set).
 * You may add new methods if you so wish.
 */
public class IntegerSet {
    // You may define new variables here if needed

    /**
     * Returns <code>true</code> if the array <code>setArray</code>
     * contains <code>element</code>.
     * <p>
     * This method <strong>must</strong> be implemented using
     * <em>recursion</em>. That is, you are not allowed to use
     * iterators, <code>for</code> loops, <code>while</code> loops
     * and similar techniques to implement member, rather you should
     * call a method defined by you recursively.
     * <p>
     * Note that the <code>null</code>
     * reference indicates that the array does not contain an element
     * at that position.
     */
    public static boolean member(int element, Integer setArray[]) {
	// You SHOULD change the body of this method
	return false;
    }

    /**
     * Inserts the integer <code>element</code> 
     * in the <code>setArray</code> array argument, if it not
     * already present (and if there is space to insert it).
     * Integers can be inserted in array cells which contain <code>null</code>.
     * <p>
     * Returns <code>true</code> if the element was inserted,
     * and <code>false</code> otherwise (the integer was already in
     * the array, or there is no space in the array).
     * <p>
     * This method <strong>must</strong> be implemented <em>without</em>
     * using recursion. That is, you should use a <code>for</code> loop, 
     * or a <code>while</code> loop to implement insert, you should
     * not use recursion nor should you use iterators.
     * <p>
     * You <em>may</em> however call your method
     * <code>member</code> to check whether the array
     * already contains the element to add.
     */
    public static boolean insert(int element, Integer setArray[]) {
	// You SHOULD change the body of this method
	return false;
    }
}



什么是递归?

数学计算机科学中,是指在函数的定义中使用函数自身的方法。如:从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?「从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?『从前有座山,山里有座庙,庙里有个老和尚,正在给小和尚讲故事呢!故事是什么呢?……』」


题中要求,用递归方法来在数组中查找元素是否存在,若存在返回true,反之则返回false。并要求在此数组中插入元素,条件为有位置,且该数组不包含与此元素相同的元素,并将是否可以插入结果返回。


贴上完整程序

public class IntegerSet {
    // You may define new variables here if needed
    static int i=0;
 public static boolean member(int element, Integer setArray[]) {
	// You SHOULD change the body of this method

    	boolean sol = false;
    	
    	if(i<setArray.length){
    		if(setArray[i] != null && setArray[i] == element){
    			return true;
    		}
    		else{
    			i++;
    			 sol = member(element,setArray);
    		}
    	}
    	i=0;//将i归0,方便下次寻找
    	return sol;
    }
 public static boolean insert(int element, Integer setArray[]) {
	// You SHOULD change the body of this method
    	boolean sol = false;
    	boolean aux = member(element, setArray);//寻找此element是否存在于数组中
    	for(int i=0;i<setArray.length && !sol && !aux;i++){
    		if(setArray[i]==null){
    			setArray[i]=element;//若此位置为空,则插入element
    			sol=true;
    		}
    	}
    	return sol;
    }
}


附上tester

package integerSet;

/**
 * This class provides methods to test your implementation of
 * the <code>IntegerSet</code> implementation.
 */
public class Tester {
    /**
     * Runs the test suite.
     */
    public static void main(String args[]) {
	doTest();
    }

    /**
     * Executes the test suite.
     */
    public static void doTest() {
	Integer s0[] = {0,1,2,3,4};

	if (!IntegerSet.member(3,s0)) {
	    System.out.println("3 should be a member of {0,1,2,3,4}");
	    throw new Error();
	}

	if (IntegerSet.member(7,s0)) {
	    System.out.println("7 should not be a member of {0,1,2,3,4}");
	    throw new Error();
	}

	if (IntegerSet.insert(7,s0)) {
	    System.out.println
		("it should not be possible to insert 7 in {0,1,2,3,4}");
	    throw new Error();
	}

	if (IntegerSet.member(7,s0)) {
	    System.out.println("7 should not be a member of {0,1,2,3,4}");
	    throw new Error();
	}


	Integer s1[] = {4,1,2,null,3};

	if (!IntegerSet.member(4,s1)) {
	    System.out.println("4 should be a member of {4,1,2,null,3}");
	    throw new Error();
	}

	if (!IntegerSet.member(3,s1)) {
	    System.out.println("3 should be a member of {4,1,2,null,3}");
	    throw new Error();
	}

	if (IntegerSet.member(7,s1)) {
	    System.out.println("7 should not be a member of {4,1,2,null,3}");
	    throw new Error();
	}

	if (!IntegerSet.insert(7,s1)) {
	    System.out.println
		("it should be possible to insert 7 in {4,1,2,null,3}");
	    throw new Error();
	}

	if (!IntegerSet.member(7,s1)) {
	    System.out.println("7 should be a member of {4,1,2,7,3}");
	    throw new Error();
	}

	if (IntegerSet.insert(7,s1)) {
	    System.out.println
		("it should not possible to insert 7 in {4,1,2,7,3}");
	    throw new Error();
	}

	if (!IntegerSet.member(7,s1)) {
	    System.out.println("7 should be a member of {4,1,2,7,3}");
	    throw new Error();
	}

	
	Integer s2[] = {};
	
	if (IntegerSet.member(1,s2)) {
	    System.out.println("1 should not be a member of {}");
	    throw new Error();
	}

	if (IntegerSet.insert(1,s2)) {
	    System.out.println
		("it should not possible to insert 1 in {}");
	    throw new Error();
	}

	if (IntegerSet.member(1,s2)) {
	    System.out.println("1 should not be a member of {}");
	    throw new Error();
	}

	Integer s3[] = {null};
	
	if (IntegerSet.member(-1,s3)) {
	    System.out.println("-1 should not be a member of {null}");
	    throw new Error();
	}

	if (!IntegerSet.insert(-1,s3)) {
	    System.out.println
		("it should be possible to insert -1 in {null}");
	    throw new Error();
	}

	if (!IntegerSet.member(-1,s3)) {
	    System.out.println("-1 should be a member of {-1}");
	    throw new Error();
	}
	System.out.println("OK!");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值