hmm ... I see. Thanks for the info.
I have another question here : How am I going to delete a record(containing data) in an array ?
Thanks very much.
Maybe?Code:ardObject[0] = null;
If I were to use that solution, will I get an error if I iterate through the array ?
Can you suggest some suitable solution for me to delete a particular object , containing fields of data, inside an array ?
Thanks.
Well if you try to access that item then yes.
Code:for (int i=0;i<length;i++) { if (arr[i] == null) { continue; // no item here so ignore this entry } // process item here }
Wrote useful little function, that removes element from array and fills the hole with other elements... so if [1,2,3,4,5] and "3" gets removed, array will be [1,2,4,5,null]
here's another one for inserting. it will push elements forward and if array is too small, then last one will be lost.Code:public static void removeIndex(Object array[], int index){ if(index >= array.length || index < 0) return; //wrong index int b = 0; for(int i = 0; i < array.length; i++){ if(i == index) b = 1; else{ array[i-b] = array[i]; } } array[array.length-1] = null; }
Code:public static void insertIndex(Object ob, Object array[], int index){ if(array.getClass().getComponentType() != ob.getClass()) return; //object and array doesn't match. if(index >= array.length || index < 0) return; //wrong index for(int i = array.length-1; i >= 0; i--){ if(i > index) array[i] = array[i-1]; else if(i == index){ array[i] = ob; break; } } }
Last edited by Sinipull; 07-07-2009 at 08:42 AM.
Thanks very much guys. I really appreciate it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks