Search code examples
javamedian

Finding the middle number(s) of an arrayList


Given the Intgers:

1)ArrayList1: 3456

2) ArrayList2: 78910

What would be the best way to find the middle numbers if these were imputed as integers of an ArrayList?

I was thinking a.size()/2 (given a is the name of the ArrayList)

i want too return the middle number(s) for the set of integers for the fist example this would be 45 and for the second example this would be 891

What would be the checks if the ArrayList were even or odd? i was thinking if the size of the Arraylist is odd then add the next integer to the list of integers? Ive tried this ut but i keep going in circles. i also don't want to sort my list.

Any Clues?


Solution

  • If i understand correctly you just need to cut off first and last...

    out.remove(0);
    out.remove(out.size()-1);
    

    But first test that your list size is bigger then 2.