Home      All Games      Math Games      Word Games      Daily      Reference      Miscellaneous      Junior!      Problems



Beginner Programming Tips

Beginner Programming Tips and Tricks

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.

Index Previous    Next    Teachers    About   

AddThis Social Bookmark Button     Link To This Page

Removing An Array Element

Suppose you have an array of 10 Integers, and you want to remove one of those elements from your array. How do you do it? The code snippet below shows a subroutine which does the job. Take a look at it, then we'll talk through it.

Dim MyArray(9) As Integer

'Remove element from the array
Sub RemoveElement(Index As Integer)
   Dim I As Integer
   For I = Index To UBound(MyArray) - 1
      MyArray(I) = MyArray(I + 1)
   Next I
   ReDim Preserve MyArray(UBound(MyArray) - 1)
End Sub

What is happening here? Simple. Index is the variable indicating which element you want to remove from the array. The For-Next loop walks through the array, starting at that point, and going to the end of the array, replacing each array element with the following array element. This has the effect of removing the unwanted element (overwriting it with another element) and sliding everything else up one index in the array.

Notice that the For-Next loop stops one element short of the end of the array. There is a good reason for this; if we went all the way to the end, including the last element, the following line:

MyArray(I) = MyArray(I + 1)

would have produced an error, since (I + 1) would be an invalid array index.

Of course, when you are done, your array has an unneeded index at the end, so you can use the "ReDim" command to knock out that last array element.

Is that the quickest way to do it? No. There is a quicker way, but for this second method to work, you have to have an array in which the order of the elements doesn't matter. Take a look at the code, and you'll see what I mean.

Dim MyArray(9) As Integer

'Remove element from the array
Sub RemoveElement(Index As Integer)
   MyArray(Index) = MyArray(UBound(MyArray))
   ReDim Preserve MyArray(UBound(MyArray) - 1)
End Sub

Do you see what is happening here? Notice there's no longer a For-Next loop. All I'm doing is snagging the very last array element and putting it in place of the array element I want to remove. Then I use the ReDim command to get rid of the unneeded array element at the end.

Other Scenarios

  1. Write a subroutine that will always delete the first array element
  2. Write a subroutine that will always delete the last array element
  3. Write a subroutine that will delete 2 array elements, starting at Index
  4. Write a subroutine that will delete N array elements, starting at Index

Index Previous    Next    Teachers    About   

AddThis Social Bookmark Button     Link To This Page
"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 


Want To Try Something Completely Different?

The Treasure Hunt

Pirate's Map

Easter Eggs



Site Features

Word Games

Word Games

Math Games

Math Games

Daily Puzzles

Daily Puzzles

Brainfood

Brainfood

Math Problems

Problems

Miscellaneous

Miscellaneous


 
Search For More Resources

Search For More Educational Resources

Find more educational, problem solving, and puzzle resources using the Google safe-search below.

Google

Member Features
Login
 

Picture Word
Four-Scramble
Word Search
Blackberry Game
Telephone Game
Cheater Hangman
Word Grid
Secret Word
Scrambled Word
One of These
Hangman
 

Entrapment
Adders!
Side By Side
One To Ten
Sub Triangles
Magical Squares
Math Scramble
Secret Number
Secret Number 2
Fractional Hi Lo
Concentration
Monty Hall Game
 

Trio Match
Treasure Hunt
Pirate's Map
Fizziks Tilt
Zero Gravity
Easter Egg Hunt
Quad Puzzle
Tic Tac Toe
Rotating Block
 

Codes
Slick Math!
Programming
Search It Out!
 
Secret Word
Scrambled Word
Secret Number
Word Grid
 
Brainfood
Math HS
Maine Page
Calculus Page
 
Contact
About
Related Sites
Link to TPS
 

Bookmarking and Linking
Bookmark/Link


Home      All Games      Math Games      Word Games      Daily      Reference      Miscellaneous      Junior!      Problems