Saturday, September 27, 2014

Arrays: Create arrays dynamically

Ques:Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid.

Ans:
public int[] fizzArray3(int start, int end) {
   int[] temparray=new int[end-start];
        int tot=end-start;
        for(int i=0;i<tot;i++){
                      temparray[i]=start;
                              start++;
        }
        return temparray;
}

No comments:

Post a Comment