Difference between revisions of "Talk:Arrays (Papyrus)"
Jump to navigation
Jump to search
→For beginners who didn't major in CS
imported>Cdcooley (→For beginners who didn't major in CS: new section) |
imported>Cdcooley |
||
Line 31: | Line 31: | ||
Programmers have to be even more obsessive because computes are dumb and need every little detail written down. | Programmers have to be even more obsessive because computes are dumb and need every little detail written down. | ||
===Defining arrays=== | |||
First the computer needs to know what kind of things will be in the list, that it will be a list, the name of the list. | First the computer needs to know what kind of things will be in the list, that it will be a list, the name of the list. | ||
Line 37: | Line 38: | ||
</source> | </source> | ||
===Declaring arrays=== | |||
But many times we need to also tell the computer to make enough storage space to hold the number of items we plan to store. | But many times we need to also tell the computer to make enough storage space to hold the number of items we plan to store. | ||
Line 42: | Line 44: | ||
FoodItem[] GroceryList = new Fooditem[4] | FoodItem[] GroceryList = new Fooditem[4] | ||
</source> | </source> | ||
===Setting individual values=== | |||
Then the items are stored in the numbered locations, but it's easier for the computer to start counting at zero instead of one, and has to be reminded of which list every single time. | Then the items are stored in the numbered locations, but it's easier for the computer to start counting at zero instead of one, and has to be reminded of which list every single time. | ||
Line 51: | Line 55: | ||
GroceryList[3] = "Bananas" | GroceryList[3] = "Bananas" | ||
</source> | </source> | ||
===Checking the length=== | |||
Finding out how many items are in the list is easy. (Actually it's how many slots are available, even if some are blank.) | Finding out how many items are in the list is easy. (Actually it's how many slots are available, even if some are blank.) | ||
Line 57: | Line 63: | ||
GroceryList.length | GroceryList.length | ||
</source> | </source> | ||
===Getting individual values=== | |||
Getting the values back isn't bad as long as you remember that the computer starts counting at zero. | Getting the values back isn't bad as long as you remember that the computer starts counting at zero. | ||
Line 63: | Line 71: | ||
FoodItem TheThirdItem = GroceryList[2] | FoodItem TheThirdItem = GroceryList[2] | ||
</source> | </source> | ||
===Creating functions that expect arrays=== | |||
When you need to tell the computer to expect a list, you put the square brackets after the name of the type of items the list will hold. | When you need to tell the computer to expect a list, you put the square brackets after the name of the type of items the list will hold. | ||
Line 69: | Line 79: | ||
Function BuyThese(FoodItem[] ShoppingListForToday) | Function BuyThese(FoodItem[] ShoppingListForToday) | ||
</source> | </source> | ||
===Sending whole arrays to a function=== | |||
But to do something with an entire list you would just use it's name. | But to do something with an entire list you would just use it's name. | ||
Line 75: | Line 87: | ||
BuyThese(GroceryList) | BuyThese(GroceryList) | ||
</source> | </source> | ||
===Examples of working with every item in the array | |||
The real power of lists only appears when you need to do the same thing with every item in the list. We can loop through all of the items in the list by their index position. | The real power of lists only appears when you need to do the same thing with every item in the list. We can loop through all of the items in the list by their index position. | ||
Line 108: | Line 122: | ||
</source> | </source> | ||
===Other considerations=== | |||
But passing that list into the BuyThese function really is like handing someone a list. If code inside that function changes or removes some of the items, then they are changed everywhere that list is referenced. We aren't making copies of the list, just letting different parts of the code see it (and some may call it different things, but it's the same list). | But passing that list into the BuyThese function really is like handing someone a list. If code inside that function changes or removes some of the items, then they are changed everywhere that list is referenced. We aren't making copies of the list, just letting different parts of the code see it (and some may call it different things, but it's the same list). | ||
So after we've called "BuyThese(GroceryList)" we'll be able to see if anything wasn't available. | So after we've called "BuyThese(GroceryList)" we'll be able to see if anything wasn't available. | ||
Line 121: | Line 136: | ||
</source> | </source> | ||
Some special limitations are that each list can only have up to 128 items, you must use a literal number with the "new" command, and that you can't have an array of arrays. | |||
--[[User:Cdcooley|Cdcooley]] 23: | --[[User:Cdcooley|Cdcooley]] 23:24, 25 May 2012 (EDT) |