Saturday, April 9, 2011

remove numbers that are outside the range from int array java

The code below removes any negative numbers from the supplied array that are outside the range and returns a new array.

static int[] remove_outside_range(int array[] , int lower_bound , int upper_bound){

ArrayList<Integer> al = new ArrayList<Integer>();

    for(int i=0; i < array.length; i++){

    if(array[i] > lower_bound){
    al.add(array[i]);
    }

    if(array[i] < upper_bound){
    al.add(array[i]);
    }

}

    int new_array[] = new int[al.size()];

    for(int i =0 ; i < al.size() ; i++){
    new_array[i] = al.get(i);
    }

    return new_array;
}

No comments:

Post a Comment