编 辑 新 闻
新闻类型:
编程天地
编程天地
摄影天地
海贝官方
文学
电脑技术
创业天地
旅游天地
健康生活
游戏动漫
法律法规
商品欣赏
汽车世界
净化工程
JQ与JS
JQ与JS
APP
asp.net
系统
数据库
UI
JAVA
新闻主题:
 
搜索
 
前台
商品编号:
发 表 人:
关键字:
用空格隔开
新闻内容:
(小于1200字时 <br 分页)
<pre class="hljs cpp">bool hasDuplicate = false; int[] a = new int[] {1, 2, 3, 4};int[] b = new int[] { 5, 6, 1, 2, 7, 8 };</pre><p>我需要将数组A的所有元素与数组B的元素进行比较,如果B中有重复的元素,则将true设置为hasDuplicate。</p><p><br/></p><p><br/></p><p>我使用“IndexOf”和“foreach”循环来创建它。 (注意:前3个“字符串”行只是一个如何创建数组并使其成为正确格式的示例)。 如果要比较2个数组,它们将以分号分隔,但最后一个值后面没有一个数组。如果在数组的字符串形式中附加一个分号(即a; b; c变为a; b; c;),则可以使用“x;”匹配无论它处于什么位置:</p><pre class="hljs cs">bool found = false;string someString = "a-b-c";string[] arrString = someString.Split('-');string myStringArray = arrString.ToString() + ";";foreach (string s in otherArray) { if (myStringArray.IndexOf(s + ";") != -1) { found = true; break; } }if (found == true) { // ....}</pre><p><br/></p><pre class="hljs cpp">public bool Checking() { bool hasDuplicate = false; int[] a = new int[] { 1, 2, 3, 4 }; int[] b = new int[] { 5, 6, 1, 2, 7, 8 };int count = a.Intersect(b).Count(); if (count >= 1) hasDuplicate = true;return hasDuplicate;</pre><p><br/></p><p>我用<code class="hljs bash"><span class="hljs-keyword">for</span></code>循环完成了它。关键是我们将每个成员与数组<code class="hljs">b</code>中的成员进行比较。因此,首先将<code class="hljs css"><span class="hljs-selector-tag">a</span><span class="hljs-selector-attr">[0]</span></code>与数组<code class="hljs">b</code>中的每个成员进行比较,然后转到<code class="hljs css"><span class="hljs-selector-tag">a</span><span class="hljs-selector-attr">[1]</span></code>并执行相同操作,依此类推,直到找到匹配项。</p><pre class="hljs cpp">bool hasDuplicate = false;int[] a = new int[] { 1, 2, 3, 4 };int[] b = new int[] { 5, 6, 1, 2, 7, 8 };for (int i = 0; i < a.Length; i++) { for (int j = 0; j < b.Length; j++) { if (a[i] == b[j]) { hasDuplicate = true; } } }</pre><p><br/></p><p>不是最高效,但可能最容易理解的方法是这样的:</p><pre class="hljs cs">foreach (int _a in a) { // iterate through all elements in array a (as _a) foreach (int _b in b) { // iterate through all elements in array b (as _b) if (_a == _b) { // if we've got a duplicate hasDuplicates = true; // store that for later on break; // immediately leave this loop (no point in further looking up) } } if (hasDuplicates) { // if we've got a duplicate break; // leave this loop as well (no point in further looking up) } }</pre><p><a href="https://oomake.com/question/3018141" _src="https://oomake.com/question/3018141">https://oomake.com/question/3018141</a> </p>
操作:
跳过导航链接。
净化工程
洁净室
汽车世界
安全常识
汽车养护
品牌欣赏
商品欣赏
商品评测
高逼格
商品对比
法律法规
法律法规
民生政策
游戏动漫
动漫天地
游戏欣赏
Flash游戏
健康生活
杂文
职务分类
便民服务
影视天地
教育
生活常识
饮食文化
偏方药方
养生之道
圣经领悟
开心一笑
品牌黑白帮
妇婴
旅游天地
购物天堂
中国村镇
旅游景点
旅游咨询
创业天地
创业故事
公司管理
创业项目
工作技巧
展会
资料
电脑技术
维修
电脑知识
文学
人间万象
历史
古诗词
人生感悟
海贝官方
商品欣赏
招标信息
海贝新闻
招聘专栏
关于我们
行情动态
海贝娱乐
海贝项目
会员礼品
商品报价
特惠信息
摄影天地
古风古韵
花鸟鱼虫
秀美山河
静物拍摄
风土人情
节日祝福
城市风光
美女摄影
编程天地
JAVA
UI
数据库
系统
asp.net
APP
JQ与JS