SRL Archive - Outdated Tutorials

Arrays!


Raskolnikov

2008-07-03 13:43:37

Hi,

What are arrays? It's basically a type where you can store multiple integers, strings, booleans, ect.

Example 1
var
food: array [0..4] of integer;
food[0] := 0;
food[1] := 1;
food[2] := 2;

Okay, now what does this mean? This is declaring part of the array. The first integer is worth zero, the second worth one, and so on. You can access one part of the array that stores an integer. You can do the same thing with strings, booleans, and extended numbers.

There are other ways to declare an array of integers and strings:

Example 2
var  
food: TIntegerArray; //same thing as array of integer
otherfood: TStringArray; //same thing as array of string

Let's say you don't know how long the array is going to be? This could happen if a function outputs an array. All you have to do is:

Example 3
food: array of integer;

Notice there is no [0..5]…

What if you need to know how long the array is? You may need this for special functions or use with TPointArrays.

Example 4
GetArrayLength(food);

Why is there a minus one after getarraylength? During the for to do loop, it does an extra loop because you start on zero. So you have to do getarraylength - 1 to do the correct amount of looping.

And run it through a for to do loop? You would need this for other functions and TPointArrays or T2PointArrays

Example 5
TheLength := GetArrayLength(food) - 1;
for i := 0 to TheLength do
Something;

Now I've been talking about using TPointArrays. But how do they fit in with TPA's? Here, I'll show you. Here's where we get more indepth, so beginners beware. Let's say you have a TPointArray and a T2DPointArray.
var
Happy: TPointArray;
HappyGroups: T2DPointArray; // is equal to array of TPointArray
i, length: integer; //these make their appearence later
MidTPoint: TPoint; //this also comes in later

Then you run a TPAtoATPAEx on Happy.

HappyGroups := TPAtoATPAEx(Happy, 3, 3);

Now you want to search all of the TPAs in HappyGroups. This is where what I have taught you comes in:

length := GetArrayLength(HappyGroups) - 1;
for i := 0 to length do //here's where they come in!
begin
MidTPoint := MiddleTPA(HappyGroups[i]); //And this one!
end;

The first line find the length of the array of TPointArray. The second is a for to do loop. The fourth is the most valuable to the tutorial. HappyGroups means that every cycle it will find the middle tpoint of the TPointArray of what i is equal to. The number in the [] is the TPointArray it will find the middle point of.

This is my second tutorial. I hope you liked it and you learned something :)
Cut em2 it

Hobbit

2008-07-03 15:34:22

Good Tutorial… Seems a little bit vague and rushed though. But still good as a whole.

Raskolnikov

2008-07-03 20:30:03

Thanks Hobbit! I'll make it better.

Cut em2 it

Scaper

2008-07-23 22:39:04

not bad like hobbit said could be a lil better but good over all for noobs

Bionicle

2009-09-20 21:11:23

if i knew nothing about arrays, this would be very confusing. I understood most of it, but i have past with arrays. Add more examples, and explain in more detail the def'n of an array.
fix up a bit please :)