vb.net - Get an item's id from an array and remove it from the array -
so need item's position in array , remove array.
names = {"bob", "jeff", "harry", "carl"}
i ask "jeff" example , output 1.
i going using item's position remove same position in array. sorting algorithm need code (without array.sort(names) ) if there better way appreciate it.
you can "remove" array, it's not call efficient. have resize array.resize
, believe in background recreates it.
dim arrnames string() = {"bob", "jeff", "harry", "carl"} dim snametosearchfor string = "jeff" integer = 0 arrnames.count - 1 'make sure you're not trying set last value in array if <> arrnames.count - 1 arrnames(i) = arrnames(arrnames.count - 1) end if 'resize array removing 1 , exit loop array.resize(arrnames, arrnames.count - 1) exit next
like said, it's not ideal, works.
as alternative, might same above in background...
dim arrnames string() = {"bob", "jeff", "harry", "carl"} dim snametosearchfor string = "jeff" dim nindex integer = array.indexof(arrnames, snametosearchfor) 'if it's -1 didn't find object in array if nindex <> -1 'check see if it's @ end of array if nindex <> arrnames.count - 1 arrnames(nindex) = arrnames(arrnames.count - 1) end if array.resize(arrnames, arrnames.count - 1) end if
Comments
Post a Comment