第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查屬性值升序并查找重復項?

如何檢查屬性值升序并查找重復項?

C#
梵蒂岡之花 2021-04-29 13:12:38
這是一個示例xml<?xml version="1.0"?><catalog>    <book id="bk101">        <author>Gambardella, Matthew</author>        <title>XML Developer's Guide</title>        <genre>Computer</genre>        <price>44.95</price>        <publish_date>2000-10-01</publish_date>        <description>An in-depth look at creating applications        with XML.</description>    </book>    <book id="bk102">        <author>Ralls, Kim</author>        <title>Midnight Rain</title>        <genre>Fantasy</genre>        <price>5.95</price>        <publish_date>2000-12-16</publish_date>        <description>A former architect battles corporate zombies,            an evil sorceress, and her own childhood to become queen        of the world.</description>    </book>    <book id="bk102">        <author>Corets, Eva</author>        <title>Maeve Ascendant</title>        <genre>Fantasy</genre>        <price>5.95</price>        <publish_date>2000-11-17</publish_date>        <description>After the collapse of a nanotechnology            society in England, the young survivors lay the        foundation for a new society.</description>    </book>    <book id="bk103">        <author>Corets, Eva</author>        <title>Oberon's Legacy</title>        <genre>Fantasy</genre>        <price>5.95</price>        <publish_date>2001-03-10</publish_date>        <description>In post-apocalypse England, the mysterious            agent known only as Oberon helps to create a new life            for the inhabitants of London. Sequel to Maeve        Ascendant.</description>    </book></catalog>如何檢查節(jié)點中屬性ID的值是否<book>按升序排列,還以最簡單的方式查找其中是否存在重復值。我做了static void Main(string[] args){    XDocument myfile = XDocument.Parse(File.ReadAllText(@"D:\sample_xml.xml"));    var check = myfile.Descendants("book").Select(a => a.Attribute("id").Value.Substring(2)).ToArray();但這并不能說明重復的值...我該怎么做?另外,是否有可能在屬性id中找到缺失值(如果有),例如,如果存在bk109而下一個是bk112,則程序?qū)@示bk110和bk111缺失。
查看完整描述

2 回答

?
小怪獸愛吃肉

TA貢獻1852條經(jīng)驗 獲得超1個贊

您已經(jīng)快到了-比較結(jié)果為0(即值與上一個相同)時,您將執(zhí)行“嚴格遞增,不重復”和“遞增,允許重復”之間的唯一區(qū)別。


如果比較的結(jié)果不是,您只需要更改IsSortedAscending返回的方法即可:false>= 0> 0


public static bool IsSortedAscending(string[] arr)

{

    for (int i = arr.Length - 2; i >= 0; i--)

    {

        // Fail if this ID is equal to or bigger than the next one.

        if (arr[i].CompareTo(arr[i + 1]) >= 0)

        {

            return false;

        }

    }

    return true;

}

(您也可以使用Skip和Zip作為另一種成對比較元素的方式,但這是稍有不同的事情。)


請注意,如果您的數(shù)字長度不同,當前您的代碼可能會失敗。例如,考慮ID“ bk99”和“ bk100”。它將比較“ 99”和“ 100”作為字符串,并確定“ 99”在“ 100”之后。


如果您的ID始終是真的“ bk”,后跟一個整數(shù),我會盡早解析它們:


var ids = myfile.Descendants("book")

                .Select(a => a.Attribute("id").Value.Substring(2))

                .Select(id => int.Parse(id))

                .ToArray();

然后,您將更改方法以接受int[]而不是string[]。


到那時,檢查“缺失” ID也更容易-字符串形式,沒有“缺失” ID的真實概念,因為您可能會有“ bk101”,“ bk101a”,“ bk101c”-是“ bk101b” ”在那里不見了嗎?如果是這樣,那么“ bk101aa”呢?使用整數(shù),它要簡單得多。


獲得整數(shù)ID數(shù)組后,就可以使用數(shù)組的長度來檢查是否缺少任何值:


if (ids.Length > 0 ids.Length - 1 != ids.Last() - ids.First())

{

    Console.WriteLine("At least one ID is missing");

}

誠然,那不會告訴您缺少哪個ID。


查看完整回答
反對 回復 2021-05-08
  • 2 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號