Finding max Int using recursion in Java

Hello I'm new to java so please be gentle. I'm sure the code seems long winded and ugly but we all start somewhere.

I'm trying to find the largest Number in an array using recursion. My output is 0 for any array the default -1 is working.

Any help to steer me in the right direction would be a great help

Thank you all in advance-

public int maxInt(MyList<Integer> m){
int result = 0;
int e0;
int len = m.length();
int e1;

if (len == 0) {
    result = -1;
    }
else if(len == 1) {
    e0 = m.getElement(0);
    result = e0;
    }   
else if(len == 2) {
    e0 = m.getElement(0);
    e1 = m.getElement(1);
    if (e0 > e1) {
        result = e0;
    }else {
        result = e1;
        }
    }
else if(len > 2) {
    e0 = m.getElement(0);
    e1 = m.getElement(1);
    if (e0 <= e1){
        m.removeElement(0);
        result = maxInt(m);
        m.addElement(0, e0);
        }       
    }   
return result;
}


from Recent Questions - Stack Overflow https://ift.tt/2IWTGtP
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)