//1、实现int 数组去重 (获得一个新数组不重复的元素)
int[] arr=new int[] { 1, 1, 2, 3, 4, 5, 6, 5 };
int[] arrs=new int[] { };
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i]==arr[j]) {
var a=arrs.push();//报错代码
}
}
}
以上代码在VS202软件中的控制台应用程序中执行
你这基础一点都不行,需要把 C# 类型概念补补,但看你问问题比较诚恳,我给出两种方式:
System.Linq
using System.Linq;
,你就可以使用Linq
,至于什么是Linq
可以自行百度科普,代码如下:using System;
using System.Linq;
namespace test
{
class MainClass
{
public static void Main(string[] args)
{
int[] arr = { 1, 1, 2, 3, 4, 5, 6, 5 };
var a = arr.Distinct(); //该函数可以去重,返回非重复集合
foreach(var item in a)
{
Console.WriteLine(item);
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
namespace test
{
class MainClass
{
public static void Main(string[] args)
{
int[] arr = { 1, 1, 2, 3, 4, 5, 6, 5 };
List<int> a = new List<int>();
foreach(var item in arr)
{
if (a.Contains(item))
continue;
a.Add(item);
}
foreach (var item in a)
{
Console.WriteLine(item);
}
Console.Read();
}
}
}
还有,你貌似把C/C++
和C#
混在一块了,他们两个只是语法稍微像一点,本质上完全不一样。C
和C++
虽然不是同一种语言,但C++
有些东西是兼容C
的。
好的,我已经写出来了,但你发的比我写的要简便多了。
Linq的语法我以为只能用来查询数据库中的内容,没有深入了解。
还有谢谢解答我的疑惑。
报啥错啊???
是不是因为arrs
这个数组没有初始化数组大小
报错
代码:CS1061
说明:int[]未包含push的定义,并且找不到可接受第一个int[]类型参数的可访问扩展方法push(是否缺少using指令或程序集引用)
@宋人鱼:
C#? 没用过。。。
你可以试试这样行不行?
int[] arr=new int[] { 1, 1, 2, 3, 4, 5, 6, 5 };
int[] arrs=new int[arr.Length];
int index = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i]==arr[j]) {
arrs[index] = arr[i];//用索引给数组赋值
index++;
}
}
}
@飒沓流星: 这样不行啊,我循环了arrs数组中的数据,输出不出来
@宋人鱼:
抱歉,那就不知道了,C#我不太会
@飒沓流星: 没事的,每个人都有擅长的领域,不可能全能。还有谢谢你的回答。
lamda表达式就能实现吧 你了解一下
好的,知道了
数组要么初始化,要么给大小,你这不给,肯定报错啊,要么就不要用数组,就List
好的,知道了