For(each) loops and Arrays

For(each) loops and Arrays

Postby joppiesaus » 29 Sep 2013, 15:22

Hey! I made a tutorial.
Let's first start off with a array:
A array is a couple of variables in a same variable(That's how I see it).

You can make a array 2 ways:
Code: Select all
            string[] arrayString = new string[100]; // Make a array with 100 strings in it!
            int[] arrayInt = new int[] { 0, 100, 34, 10, 3948, 4, 1, 23 }; // Make a array and directly set the variables; You don't have to set the length.

            // You can do this with ALL types! even classes!
            Robot[] robots = new Robot[10];
            bool[] booleans = new bool[] { true, false, true };


In the first case, (string[] arrayString) the variables have not been set.
In the second case, all variables have been set.

To call or set a variable, you can do this:
Code: Select all
arrayString[0] = "Peter"; // Set the first variable in the array to Peter.
int test1 = arrayInt[2]; // Get the third variable in the array(which is 34) and set it.
int test2 = arrayInt[8902]; // This will throw IndexOutOfRange exception. The array isn't that long!
int test3 = arrayInt[test1 + 4]; // You can also call a array by anohter variable!


The LENGTH(stringArray.Length) is the total length, started by 1. Just like you count. But if you call the variable, DO NOT count from one, but from zero bacause:
All arrays are zero-based!

You may think this is all pretty useless. It does pretty much the same. But... we have loops! :D
You can set up a foreach to do things for each thing in a array(or list, google it.);
You can set up a for loop to do the same as foreach, but different.
You can set up a loop two ways:

1: Use a foreach loop.
Code: Select all
            foreach (string s in arrayString)
            {
                //Your code goes here! You can do with it anything!
                //The reason why I must set a to s, is because I can't modify s. That's the con of foreach.
                //Also, I can't say something like: At the third time this executes, I want to do something different.
                //Well, it can, but with only extra code.
                string a = s;
                if (a == null)
                    a = "NOTHING!";

                Console.WriteLine(a);
            }


2: Use a for loop.(I recommend that more)
Code: Select all
//integer i = 0, if i is smaller than arrayInt.Length(not equal or smaller, than you get indexoutofrange exception, because all arrays are zero-based), i++           
            for (int i = 0; i < arrayInt.Length; i++)
            {
                Random rand = new Random(Guid.NewGuid().GetHashCode()); //Ultra-Random random!
                arrayInt[i] += rand.Next(0, 100);

                if (i == 5 - 1) // if it is the fifth variable in the array,
                {
                    arrayInt[i] = 707;
                }

                Console.WriteLine(arrayInt[i].ToString());
            }


So, with this technique, we can make a array with millions of variables in it with a couple of lines! Isn't that awsome?
Also, you can make jagged arrays. Which is a array of an array! Or, a multidementional one!(not just one number, but two: [1,9])
Code: Select all
            string[][] s = new string[5][]; //This is tricky to initialize, check the link bellow.
            string[,] s2 = new string[1,5]; //this is less tricky!
            s[0] = new string[9];  //You see? You need to initialize the array OF an array. that's why I prefer multidementional arrays.
            s[1] = new string[130];
            s[0][0] = "Hi!";
            s[0][1] = "How are you?";
            s[1][0] = "I am a array of an array!";
            s2[0,1] = "Wow!";

            //Let's put this to the max:
            string[][,,,][][,][][,][][,,,,,,][,][,][] woot = new string[96568][,,,][][,][][,][][,,,,,,][,][,][];
            //Actually... you can't do this in one time. You need to jag the arrays!
            woot[1][4, 5, 2, 1][1][6, 7][7][9, 2][8][2, 6, 4, 6, 7, 8, 2][9, 2][3, 4][14] = "this is HUGE! Imagine how large this could be!";


continue and break
break will force a loop to... break. It just stops.
continue will skip to the next value.
Code: Select all
for (int i = 0; i <= 10; i++)
{
   if (i == 5)
   {
       break; // Stop if i is equal to 5.
   }
   Console.WriteLine(i);
   //Output: 0 1 2 3 4
}


Code: Select all
for (int i = 0; i <= 10; i++)
{
   if (i == 0)
   {
       continue; // Skip to next value if i is equal to zero.
   }
   Console.WriteLine(i); // Output: 1 2 3 4 5 6 7 8 9
}

More info here!
Hope you can do something with it! <ok>


And for the geeks whose program is to laggy caused by for loops:
Spoiler:
Last edited by joppiesaus on 03 Jan 2014, 11:48, edited 3 times in total.
Reason: Added geeky stuff
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 1 guest

cron