public class Test { /** * @param args */ public static void main(String[] args) { String[] a = {"1","2","3","4"}; String[] b = {"3","4","5","6"}; Set s = new HashSet(Arrays.asList(a)); s.retainAll(Arrays.asList(b)); System.out.println(s); }}执行结果: [3, 4]
五楼的不好用! */ 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); } 结果是[3, 5, 4] 我们希望借结果是[3, 4, 5]
比较笨的方法: import java.util.ArrayList; import java.util.List;/* * 取数组相同部分 */ public class CollectionTest {
for(Object num : list) { System.out.println(num.toString()); }
} }
String[] a = {"1","2","3","4","5","1","2"}; String[] b = {"3","4","5","6","1","2"};象这样的字符窜算几部分相同的?运行出[3, 2, 1, 5, 4]这样的结果能行吗
我晕死了用TreeSet就排序了,汗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); }}
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()); } }
果然都是学JAVA的...这是算法题..为快不是让想方法实现面向对象思想到都不错
虽然不配合该版,不过提一下C# 3.0 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)
/**
* 获取两个整型数组之间的重复元素集合
* @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));
}
}
}这是那次一个大哥写的!
http://topic.csdn.net/u/20071113/18/9fa94e23-df29-432f-8e21-b9498cb00a0d.html
再把那个帖子给你,你自己看吧!
public class Test { /**
* @param args
*/
public static void main(String[] args) {
String[] a = {"1","2","3","4"};
String[] b = {"3","4","5","6"};
Set s = new HashSet(Arrays.asList(a));
s.retainAll(Arrays.asList(b));
System.out.println(s);
}}执行结果:
[3, 4]
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);
}
结果是[3, 5, 4]
我们希望借结果是[3, 4, 5]
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());
}
}
}
String[] b = {"3","4","5","6","1","2"};象这样的字符窜算几部分相同的?运行出[3, 2, 1, 5, 4]这样的结果能行吗
{
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);
}}
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());
}
}
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)