|
-
July 31st, 2012, 08:58 PM
#4
Re: giving a value to a variable... I really don't get what i m doing wrong.
In C#, and most other popular languages, array indices are zero-based (you start counting from 0, instead from 1). So, for example, a 5 element array has elements at positions 0, 1, 2, 3, and 4. (0 to length-1).
In the first case, where I said that you should use countcardsquantity - 1, it's because arrays have a fixed size: if you're skipping elements, the skipped elements are still going to stay there. Since you must not access elements beyond the last, you must make sure that the array has enough elements when you create it.
Look at this:
Code:
i: . 1 . 3 . 5
cardquantity: [a][b][c][d][e][f] -----length: 6
| | |
| | |
| | |
| | |
m = i-1 | | |
m: 0 . 2 . 4
cardm1: [b][.][d][.][f] -----length: 5
vs this:
Code:
i: . 1 . 3 . 5
cardquantity: [a][b][c][d][e][f] -----length: 6
| | |
| __| |
| | |
| | _____|
m = (i-1)/2 | | |
m: 0 1 2
cardm1: [b][d][f] -----length: 3
Lists support [] just like normal arrays, and you can allways get an array from a list by calling the ToArray() method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|