Java 比较两个数组的值(如果存在或不存在)
我有一段代码,它检查两个数组并打印出另一个数组中不存在的值。我认为我这样做的方式并不是最有效的方式,因此有谁能提供更好的面向对象方法来用Java编写这段代码呢 谢谢Java 比较两个数组的值(如果存在或不存在),java,arrays,oop,Java,Arrays,Oop,我有一段代码,它检查两个数组并打印出另一个数组中不存在的值。我认为我这样做的方式并不是最有效的方式,因此有谁能提供更好的面向对象方法来用Java编写这段代码呢 谢谢 public class Calculate { static int [] x = {1,2,4,6,7}; static int [] y = {2,3,4,6,7}; static boolean xflag = true; static boolean yf
public class Calculate {
static int [] x = {1,2,4,6,7};
static int [] y = {2,3,4,6,7};
static boolean xflag = true;
static boolean yflag = true;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0; i<x.length; i++)
{
for (int b=0; b<y.length; b++)
{
if(x[i]!= y[b])
{
xflag= false;
}
else
{
xflag = true;
break;
}
}
if(xflag==false)
{
System.out.println(x[i] +" does not exist in array 2");
}
}
for(int i = 0; i<x.length; i++)
{
for (int b=0; b<y.length; b++)
{
if(y[i]!= x[b])
{
yflag= false;
}
else
{
yflag = true;
break;
}
}
if(yflag==false)
{
System.out.println(y[i] +" does not exist in array1");
}
}
}
}
公共类计算{
静态int[]x={1,2,4,6,7};
静态int[]y={2,3,4,6,7};
静态布尔值xflag=true;
静态布尔值yflag=true;
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
对于(int i=0;i您可以使用java集合框架,有很多函数,
这里有一个简单的例子来检查它
public static void main(String a[]){
List<String> sl = new ArrayList<String>();
sl.add("apple");
sl.add("java");
sl.add("c++");
sl.add("unix");
sl.add("orange");
sl.add("airtel");
List<String> tl = new ArrayList<String>();
tl.add("job");
tl.add("oracle");
tl.add("jungle");
tl.add("cricket");
boolean isCommon = Collections.disjoint(sl,tl);
System.out.println("Does not found any common elements? "+isCommon);
tl.add("java");
isCommon = Collections.disjoint(sl,tl);
System.out.println("Does not found any common elements? "+isCommon);
}
publicstaticvoidmain(字符串a[]){
List sl=新的ArrayList();
sl.add(“苹果”);
sl.add(“java”);
sl.add(“c++”);
sl.add(“unix”);
sl.add(“橙色”);
sl.add(“airtel”);
List tl=new ArrayList();
tl.add(“工作”);
tl.add(“甲骨文”);
tl.add(“丛林”);
tl.add(“蟋蟀”);
布尔值isCommon=Collections.disjoint(sl,tl);
System.out.println(“未找到任何公共元素?”+isCommon);
tl.add(“java”);
isCommon=Collections.disjoint(sl,tl);
System.out.println(“未找到任何公共元素?”+isCommon);
}
如果您希望实现逻辑的抽象,可以为此使用Apache的CollectionUtils
。例如:
public static void main(String[] args) {
List<Integer> list1=Arrays.asList(1,2,4,6,7);
List<Integer> list2=Arrays.asList(2,3,4,6,7);
System.out.println(CollectionUtils.disjunction(list1,list2));
}
publicstaticvoidmain(字符串[]args){
list1=Arrays.asList(1,2,4,6,7);
list2=Arrays.asList(2,3,4,6,7);
System.out.println(CollectionUtils.disconction(list1,list2));
}
您可以这样编码
List<Integer> array1 = Arrays.asList(1,2,4,6,7);
List<Integer> array2 = Arrays.asList(2,3,4,6,7);
List<Integer> disjointArray = new ArrayList<Integer>();
for (Integer value : array1) {
if (!array2.contains(value)) {
disjointArray.add(value);
}
}
List array1=Arrays.asList(1,2,4,6,7);
List array2=Arrays.asList(2,3,4,6,7);
List disjointArray=new ArrayList();
for(整数值:array1){
如果(!array2.contains(value)){
分离tarray.add(值);
}
}
然后,您可以打印分离数组或执行任何您想要的操作。使用Collection类removeAll方法
String original[] = { "1","2","3","4","6"};
String testStr[] = { "1","2","3","5","7" };
List origList = new ArrayList(Arrays.asList(original));
List testList = new ArrayList(Arrays.asList(testStr));
System.out.println(origList.removeAll(testList));
System.out.println(origList);
下面是一个使用Javas集合类的运行示例:
public class Disjunction {
public static void main(String args[]) throws UnsupportedEncodingException {
//Some data preparation
List<Integer> list1=Arrays.asList(1,2,4);
List<Integer> list2=Arrays.asList(5,2,8);
//Here calculating data1-data2 and data2-data1, collect all list items
//that are in data1 or in data2 but not in both.
List<Integer> data1 = new ArrayList<>(list1);
data1.removeAll(list2);
List<Integer> data2 = new ArrayList<>(list2);
data2.removeAll(list1);
//Merging both results. data1 contains now exclusive or of list1 and list2
data1.addAll(data2);
System.out.println("exclusive or is " + data1);
}
}
集合框架有removeAll
和retainal
方法,在这里可能很有用。但是不要直接对数组进行操作。是否保证数组包含相同数量的元素或排序?我不明白你的问题Tyler。我问你是否知道这两个数组是否总是被排序和/或合并n使用相同数量的元素。例如两个数组都包含5个元素。但是,如果您不确定要使用的数组类型或只是想实现代码重用状态,我建议使用Jayesh的答案,而不是发布另一个答案
exclusive or is [1, 4, 5, 8]