this question has answer here:
- getting sub-array existing array 20 answers
i have array list named intarray. need sublist of arraylist , know there method takes 2 parameters: starting index , count. wanna implement method taking 2 parameters: lower index , higher index. there way this?
for e.g arraylist :
int[] intarr = { 1, 2, 3, 4, 5, 6 }; so if specify method e.g displayarray(1,4) //1 lower index , 4 higher index. should display elements 1st index 4th index.
thanks!!!
first thing's first, have array, not arraylist (arraylist's dynamic , resizable). clarify on question, method should getting sub list rather displaying 1 if understand predicament correctly.
so have create local array (in should getsublist() function instead of displayarray function , return that. require simple loop:
(int = lowerindex; <= higherindex; i++){ ...read array , add values new array... } also, should consider whether or not indices passed in valid (ex. wouldn't want negative index or index greater size of array) make sure function safe potentially crashing program. might need consider 1 more parameter (since dealing arrays) prevent "bad" indices. length of array.
Comments
Post a Comment