62,614
社区成员
发帖
与我相关
我的任务
分享
string[] a = {"1","2","3","4","5","3","4"};
string[] b = {"3","4","5","6"};
string[] c = a.Intersect(b).ToArray();
foreach(string s in c)
Console.WriteLine(s)
或者:
string[] a = {"1","2","3","4","5","3","4"};
string[] b = {"3","4","5","6"};
var s=from c in b where a.Contains(c)
select c;
foreach(string s in c)
Console.WriteLine(s)
public class Test{
/**
* 获取两个整型数组之间的重复元素集合
* @param array1 数组参数1
* @param array2 数组参数2
* @return
*/
public List findSame(int array1[],int array2[]){
List result=new ArrayList();//重复元素结果集合
HashMap hashMap=new HashMap();//利用hashmap来寻找重复元素
for(int i=0;i<array1.length;i++){//将第一个数组加入hashmap
String temp=array1[i]+"";
hashMap.put(temp,temp);
}
for(int i=0;i<array2.length;i++){//遍历第二个数组
String temp=array2[i]+"";
if(hashMap.get(temp)!=null){//在已经存在第一个数组所有元素的hashmap里寻找第二数组里的元素
result.add(array2[i]);//将重复出现的元素加入结果集合
}
}
return result;
}
public static void main(String args[]){
long timeBegin=System.currentTimeMillis();
int a[] = {1, 6, 2, 8, 5, 8, 6, 9, 0};
int b[] = {4, 5, 4, 8, 7, 6, 2, 0};
//获取重复元素集合
List list=new Test().findSame(a, b);
//遍历输出重复元素
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
public class ss extends S {
public ss(double rrr){
super(rrr);
}
public double getArea(){
return 3.14*r;
}
public static void main(String[] args){
ss sss = new ss(10);
System.out.println(""+sss.getArea());
}
}
import java.util.*;
public class Test
{
public final static String POEM="This this this one. a one sb SD sd";
public static void main(String[] args) {
String[] a = {"1","2","3","4","5","3","4"};
String[] b = {"3","4","5","6"};
Set s = new TreeSet(Arrays.asList(a));
s.retainAll(Arrays.asList(b));
System.out.println(s);
}
}
import java.util.ArrayList;
import java.util.List;
/*
* 取数组相同部分
*/
public class CollectionTest {
public static void main(String[] args) {
int[] num_one={1,2,3,5,4,6};
int[] num_two={2,5,36,4,6};
someNum(num_one,num_two);
}
@SuppressWarnings("unchecked")
public static void someNum(int[] num_one, int[] num_two) {
List list=new ArrayList();
for(int num1 : num_one)
{
for(int num2 : num_two)
{
if(num1==num2)
{
list.add(num1);
}
}
}
showList(list);
}
public static void showList(List list) {
for(Object num : list)
{
System.out.println(num.toString());
}
}
}
*/
public static void main(String[] args) {
String[] a = {"1","2","3","4","5","3","4"};
String[] b = {"3","4","5","6"};
Set s = new HashSet(Arrays.asList(a));
s.retainAll(Arrays.asList(b));
System.out.println(s);
}