当前位置:编程学习 > JAVA >>

[Java]用递归判断是否为递减数组

No pinyin input method on this machine, so directly paste the code:


[java] 
/**
 * Judge whether the array is decreased
 * @param array
 * @return
 */ 
public boolean isDecrease(int[] array) { 
    if (array.length<=1) { 
        return true; 
    } 
    if (array[0]>array[1]) { 
        return isDecrease(Arrays.copyOfRange(array, 1, array.length)); 
    } 
    return false; 

 
public static void main(String[] args) { 
    int[] array = new int[]{5,4,6,2,1}; 
    SessionClass sClass = new SessionClass(); 
    boolean result = sClass.isDecrease(array); 
    System.out.println(result); 

    /**
     * Judge whether the array is decreased
     * @param array
     * @return
     */
    public boolean isDecrease(int[] array) {
        if (array.length<=1) {
            return true;
        }
        if (array[0]>array[1]) {
            return isDecrease(Arrays.copyOfRange(array, 1, array.length));
        }
        return false;
    }

    public static void main(String[] args) {
        int[] array = new int[]{5,4,6,2,1};
        SessionClass sClass = new SessionClass();
        boolean result = sClass.isDecrease(array);
        System.out.println(result);
    }

 

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,