不同的for循环增量值(Different for-loop incremental value)
我最近摆弄了一些for循环,并试图绕过它的一些逻辑部分。 虽然我在这一点上远远超出了循环的水平,但有时候我仍然倾向于有点奇怪。
但是,我发现for循环的增量值取决于你是在循环内部还是外部:
int i; for(i = 0; i < 10; i++) { System.out.println(i); // 0 ... 9 } System.out.println(i); // 10
现在,我担心有一天我会到达一个地方,我将不得不使用计数器,而不仅仅是将它与条件相匹配......
现在我想我明白为什么会这样; 在条件失败并且循环死亡之后,我正在增加,但是真的应该发生吗?
是否有任何数学/逻辑(或优雅)方式在循环终止后不能使其进入N + 1,我在这里缺少?
I recently fiddled around with some for loops, and tried to get my head around some of the logical part of it. While i'm far beyond the level of loops at this point, I still tend to get a bit weird when it comes to logic sometimes.
However, I found that the incremental value of a for loop is different depending on whether you are inside or outside the loop:
int i; for(i = 0; i < 10; i++) { System.out.println(i); // 0 ... 9 } System.out.println(i); // 10
Now, i'm afraid one day i'll come to a point where i'll have to use the counter for something more than just matching it against the condition...
Now I think I understand why this is happening; i is being incremented after the condition fails and the loop dies, but is that really supposed to happen?
Is there any mathematical/logical (or elegant) way to not make it go N+1 after loop termination, that i'm missing here?
原文:https://stackoverflow.com/questions/16625855
最满意答案
解决了asp.net mvc 4发布。 伟大的实施: http : //www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/
Solved in asp.net mvc 4 release. Great implementation: http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/
相关问答
更多-
ASP.Net WebAPI区域支持(ASP.Net WebAPI area support)[2023-04-05]
你可以把Api控制器放在你喜欢的任何文件夹中,你不需要像为MVC那样创建一个区域。 我通常做的是在MVC网站的'Controllers'文件夹内创建一个子文件夹'Api'。 只需为您的Api控制器注册路由,它就可以工作。 You can put Api controllers in any folder you like, you don't need to create an Area like you did for MVC. What I usually do is create a subfolde ... -
您应该加载问题(如上面的代码所示),并使用linq或foreach获取具有指定_id的回答项目。 所以代码将如下所示: List
list = new List (); MongoServer _server = MongoServer.Create("mongodb://localhost"); MongoDatabase _database = _server.GetDatabase("test"); var query = Query.And(Query.EQ(" ... -
您必须使用返回MongoCursor的函数。 在MongoCursor中,您可以指定要返回的字段。 var result = Db.GetCollection
("Users").FindAll(); result.Fields = Fields.Include(new [] {"Name"});; foreach (var user in result) { Console.WriteLine(user.Name); } You'll have to use a functi ... -
MongoDB查询C#驱动程序(MongoDB Query C# Driver)[2023-07-18]
您可以按如下方式构建您的查询: var query = Query.NE("ProfilePictureId", ObjectId.Empty); ObjectId.Empty返回一个由所有零组成的ObjectId 。 You can build your query as follows: var query = Query.NE("ProfilePictureId", ObjectId.Empty); ObjectId.Empty returns an ObjectId composed of al ... -
MongoServer可以保持单身。 MongoDatabase也是如此。 它们都是线程安全的,即使您每次都创建一个新的MongoServer和MongoDatabase,您也会返回相同的实例,因为只要连接字符串完全相同,它们就会缓存在下面。 MongoServer is just fine to keep as a singleton. The same is true of a MongoDatabase. They are both thread-safe and even if you create ...
-
Mongodb c#库异步(Mongodb c# library Async)[2022-07-04]
虽然您可以将其设置为异步,但这样做不会为您提供任何真正的性能提升,因为底层库不是Async。 它还有很多东西, 在这里有很好的描述。 一般的答案是“不”。 While you could make it async, doing so won't provide you any real performance gains as the underlying library isn't Async. There's a lot more to it and is described well here. T ... -
我是Linq2Rest的开发者,你的积分非常有效。 创建Linq2Rest是为了在WebAPI之外添加OData支持。 如果您已经在使用WebAPI,那么请务必坚持使用Microsoft。 它不是来自Microsoft,但OData是一个开放标准,Linq2Rest是根据标准文档实现的。 开源已经意味着Linq2Rest能够比微软更频繁地发布。 出于这个原因,它已经支持来自OData 3规范的Any / All查询,Microsoft目前不支持这些查询。 在每个上下文中注入客户端是为了减少它支持的服务的限制 ...
-
kendo ui ToDataSourceResult支持mongodb C#驱动程序(kendo ui ToDataSourceResult support mongodb C# driver)[2021-04-16]
我在我的Filter类(模型)中发现我的问题我有一个枚举,我的枚举有错误的值我将我的枚举更改为此值 public enum MyFilterOperator { Lt, Lte, Eq, Neq, Gte, Gt, Startswith, Endswith, Contains, IsContainedIn, Notsubstringof, IsNull, IsNotNull, IsEmpty, ... -
MongoDB - 使用官方.NET驱动程序保存时更改文档(MongoDB - Getting changes to a document when saving with official .NET driver)[2021-07-31]
服务器不向客户端提供任何增量,因此任何此类事情都必须在客户端完成。 C#驱动程序也没有任何此类功能。 在开始更改原始文档之前,您必须保留原始文档的副本,然后自己进行增量。 您还可以考虑使用FindAndModify,它可以返回原始文档,从而使您不必保留文档的原始副本。 The server doesn't provide any deltas back to the client, so any such thing would have to be done client side. The C# dri ... -
解决了asp.net mvc 4发布。 伟大的实施: http : //www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/ Solved in asp.net mvc 4 release. Great implementation: http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with ...