Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2011, 07:14 AM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Ch07Ex01

Me again

if (integers[i] == maxval)
{
count++;
int[] oldindicies = indices;
indices = new int[count];
oldindicies.CopyTo(indices, 0);
indices[count - 1] = i;
Debug.WriteLine(string.Format("duplicate maxima found at element index {0}.", i));

i am only confused with this line: indices[count - 1] = i;
why we use [count-1]?

Thanks in advance
 
Old November 8th, 2011, 12:18 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

[count - 1] is used to access the last element of the array.
The array is created with new int[count], and thus using count as the value for the indexer would reference an element after the last one - out of bounds. The first element of the array is referenced with an indexer value 0: [0]. The last one with [numberofelements - 1].
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
dampyr (November 8th, 2011)
 
Old November 8th, 2011, 02:41 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Thumbs up

Thank You very much for clarifying the vagueness.Currently i am on chapter 8 and,so far, this book is very usefull for beginners like me!









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.