java用递归法在数组中查找,插入
来源:互联网 发布:k60单片机介绍 编辑:程序博客网 时间:2024/05/22 17:20
先来看原题要求
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 methodreturn 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 methodreturn 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!"); }}
- java用递归法在数组中查找,插入
- Java 在数组中查找
- 折半查找 在数组中插入一个数返回index
- 【c++程序】用递增法在数组中查找数据
- Java数组-二分查找法用于元素插入有序数组,并获取元素插入的位置
- 顺序数组中插入相应的值用二分查找法
- 顺序查找法查找元素在数组中所在位置
- java用递归实现折半查找法
- Java在数组中插入数据实现方式
- 在有序数组中插入
- Java二分法在已排序数组中查找指定数
- JAVA查找该数是否在数组中
- java实现在二维数组中查找一个数
- 黑马程序员-IOS学习笔记 折半查找 在数组中插入一个数返回index
- 在数组中查找数字
- 在大数组中查找
- Java实现数组二分查找及递归二分查找
- Java-在一组有序数组中进行查找指定数及对应下标,再插入指定数,最后删除指定数。
- 基于用户投票的排名算法(六):贝叶斯平均
- oradim
- 高斯模糊的算法
- 关于QQ授权页面的post登录cookie算法
- 贝叶斯推断及其互联网应用(一):定理简介
- java用递归法在数组中查找,插入
- 贝叶斯推断及其互联网应用(二):过滤垃圾邮件
- [HDU 2222]Keywords Search[AC自动机]
- 贝叶斯推断及其互联网应用(三):拼写检查
- 【笔试-基础】FIFO、LRU、OPT这三种置换算法的缺页次数
- ViewPager多页面滑动切换以及动画效果
- poj 3020 (二分匹配)
- 优秀程序员的十个习惯
- HDU3629(凸四边形的个数)