School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,078 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,245 people online right now. Registration is fast and FREE... Join Now!




> Arrays, Arrays Made Simple

AdamSpeight2008
Group Icon



post 28 Jul, 2008 - 10:02 PM
Post #1


Single Dimension
vb

Dim TheArray(9) As Integer

This defines a fixed size array of Integers containing 10 elements. The first one is element Zero and the last element Nine
The elements can accessed
vb

For i As Integer=0 To 9
TheArray(i)=i
Next i


It is also possible to define an array which can be resized
vb

Dim TheArray() As Integer

To create an array containing 10 elements.
vb

ReDim TheArray(9)
For i As Integer=0 To 9
TheArray(i)=i
Next i


Note using ReDim this way will clear all of the contains previously stored in the array.

Now suppose we want to resize the array to contain 20 items without clearing the contents
vb

ReDim Preserve TheArray(20)


Two Dimension
Imagine you want to recreate the multiplication table.
vb

Dim TheArray(9,9) As Integer
For x As Integer=0 To 9
For y As Integer=0 To 9
TheArray(x,y)=x*y
Next y
Next x

It is also possible to use a resizable array.
vb

Dim TheArray(,)
ReDim TheArray(9,9)
For x As Integer=0 To 9
For y As Integer=0 To 9
TheArray(x,y)=x*y
Next y
Next x


Three Dimensions
A Fixed Size array.
vb

Dim TheArray(9,9,9) As Integer
For x As Integer=0 To 9
For y As Integer=0 To 9
For z As Integer=0 To 9
TheArray(x,y)=x*y*z
Next z
Next y
Next x

It is also possible to use a resizable array.
vb

Dim TheArray(,,)
ReDim TheArray(9,9,9)
For x As Integer=0 To 9
For y As Integer=0 To 9
For z As Integer=0 To 9
TheArray(x,y)=x*y*z
Next z
Next y
Next x



Note If you use ReDim with Preserve on arrays with multiple dimenstions.
You can only resize the right-most dimension,
vb

ReDim Preserve TheArray(1,2,3)


Higher Dimension
You can extend the number up to any number provided the array fits in memory.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

 
Reply to this topicStart new topic
Replies(1 - 2)
mil1234
**



post 25 Mar, 2009 - 06:17 AM
Post #2
QUOTE(AdamSpeight2008 @ 28 Jul, 2008 - 10:02 PM) *

Single Dimension
vb

Dim TheArray(9) As Integer

This defines a fixed size array of Integers containing 10 elements. The first one is element Zero and the last element Nine
The elements can accessed
vb

For i As Integer=0 To 9
TheArray(i)=i
Next i


It is also possible to define an array which can be resized
vb

Dim TheArray() As Integer

To create an array containing 10 elements.
vb

ReDim TheArray(9)
For i As Integer=0 To 9
TheArray(i)=i
Next i


Note using ReDim this way will clear all of the contains previously stored in the array.

Now suppose we want to resize the array to contain 20 items without clearing the contents
vb

ReDim Preserve TheArray(20)


Two Dimension
Imagine you want to recreate the multiplication table.
vb

Dim TheArray(9,9) As Integer
For x As Integer=0 To 9
For y As Integer=0 To 9
TheArray(x,y)=x*y
Next y
Next x

It is also possible to use a resizable array.
vb

Dim TheArray(,)
ReDim TheArray(9,9)
For x As Integer=0 To 9
For y As Integer=0 To 9
TheArray(x,y)=x*y
Next y
Next x


Three Dimensions
A Fixed Size array.
vb

Dim TheArray(9,9,9) As Integer
For x As Integer=0 To 9
For y As Integer=0 To 9
For z As Integer=0 To 9
TheArray(x,y)=x*y*z
Next z
Next y
Next x

It is also possible to use a resizable array.
vb

Dim TheArray(,,)
ReDim TheArray(9,9,9)
For x As Integer=0 To 9
For y As Integer=0 To 9
For z As Integer=0 To 9
TheArray(x,y)=x*y*z
Next z
Next y
Next x



Note If you use ReDim with Preserve on arrays with multiple dimenstions.
You can only resize the right-most dimension,
vb

ReDim Preserve TheArray(1,2,3)


Higher Dimension
You can extend the number up to any number provided the array fits in memory.



FIRST OF ALL TNX FOR THIS TUTORIAL.
LET'S SAY I RANDOMLY GENERATED 50 CHARACTERS(LETTERS) AND I PUT THEM IN AN ARRAY AS U EXPLAINED.IF I WANT TO KNOW THE POSITION FROM AN ARRAY--FOR EXAMPLE I HAVE THE "E" IN THESE GENERATED LETTERS AND I WANT TO FIND ITS POSITION--EG-8 SPACES FROM "E" TO END OF BLOCK OF LETTERS OR 20 SPACES FROM START OF BLOCK.HOW CAN I DO THIS OR I DON'T NEED TO USE AN ARRAY??TNX......AGAIN
Go to the top of the page
+Quote Post

GMorris
*



post 20 Jun, 2009 - 10:52 AM
Post #3
QUOTE
FIRST OF ALL TNX FOR THIS TUTORIAL.
LET'S SAY I RANDOMLY GENERATED 50 CHARACTERS(LETTERS) AND I PUT THEM IN AN ARRAY AS U EXPLAINED.IF I WANT TO KNOW THE POSITION FROM AN ARRAY--FOR EXAMPLE I HAVE THE "E" IN THESE GENERATED LETTERS AND I WANT TO FIND ITS POSITION--EG-8 SPACES FROM "E" TO END OF BLOCK OF LETTERS OR 20 SPACES FROM START OF BLOCK.HOW CAN I DO THIS OR I DON'T NEED TO USE AN ARRAY??TNX......AGAIN

If I might add, even though this post is old, a string of characters pretty much IS an array already. VB gives many functions for manipulating strings, and you can use the StringBuilder class to extend the functionality even more. Look up StringBuilder and see if it's right for your application. Sometimes the basic String class will be more than sufficient, and will provide enough functions to manipulate strings for most scenarios. Now, if you will be using a LOT of strings in a large application, StringBuilder can help to cut down on memory usage as well. If you are new to VB.NET, you should at least study a few tutorials on it as it differs from VB6 in many ways. If you are new to .NET programming period, it may take a little more patience and study to "get it", like it did for me! In any case, good luck to all with any programming project!

This post has been edited by GMorris: 20 Jun, 2009 - 10:56 AM
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 10:28AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month