确保数组是连续在C# [英] make sure array is sequential in C#

查看:280
本文介绍了确保数组是连续在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我们是从第三方供应商获得的整数数组。这些都意味着是连续的,但由于某种原因,他们错过了一些(这是抛出一个异常,它的食用和循环继续缺少指数)。这将导致我们的系统中的一些悲伤,我想确保我们得到的数组确实是连续的。

I've got an array of integers we're getting from a third party provider. These are meant to be sequential but for some reason they miss a number (something throws an exception, its eaten and the loop continues missing that index). This causes our system some grief and I'm trying to ensure that the array we're getting is indeed sequential.

中的数字从不同的偏移量(有时是1000,有的时候5820,其他0),但无论一开始启动,它意味着从那里走。

The numbers start from varying offsets (sometimes 1000, sometimes 5820, others 0) but whatever the start, its meant to go from there.

什么是最快的方法来验证数组是顺序?即使它的一个必要步骤,现在看来,我也有,以确保它不会花费太长的时间来验证。我目前正在开始的第一个索引时,拿起数并加1,并确保下一个索引包含等。

What's the fastest method to verify the array is sequential? Even though its a required step it seems now, I also have to make sure it doesn't take too long to verify. I am currently starting at the first index, picking up the number and adding one and making sure the next index contains that etc.

编辑: 为何在系统出现故障的原因是的方式,因为人们使用该系统它可能不总是返回令牌它被采摘最初的方式 - 长的故事。的数据不能被校正,直到获得了层不幸

The reason why the system fails is because of the way people use the system it may not always be returning the tokens the way it was picked initially - long story. The data can't be corrected until it gets to our layer unfortunately.

推荐答案

如果你确定该数组排序,没有重复,你可以检查:

If you're sure that the array is sorted and has no duplicates, you can just check:

array[array.Length - 1] == array[0] + array.Length - 1

这篇关于确保数组是连续在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆