两个二维数组,怎样判断它们两个互不包容。
也就是说:它们两个中不包含各自的元素。
(例如)
a二维数组中有
{1,2,3,4,5,6}
{3,45,5,6,7,44}
b二维数组中有
{33,56,77,9,10,80}
{6,8,9,45,88,90}
c二维数组中有
{122,55,67,99,80,199}
{777,595,97,234,898,102}
a跟b有包容元素,返回false
a跟c无包容元素,返回true
也就是说:它们两个中不包含各自的元素。
(例如)
a二维数组中有
{1,2,3,4,5,6}
{3,45,5,6,7,44}
b二维数组中有
{33,56,77,9,10,80}
{6,8,9,45,88,90}
c二维数组中有
{122,55,67,99,80,199}
{777,595,97,234,898,102}
a跟b有包容元素,返回false
a跟c无包容元素,返回true
解决方案 »
- 关于java new Date()的疑问
- 这个代码比值比的是前四位吗?
- 请问如下的多线程代码为何出现异常?
- 如何用Runtime.getRuntime().exec来打开某个压缩文件,并解压到指定位置?
- java bean的一点疑问
- 急求专业的报表打印工具
- 抢答题: 运行java小程序 需要虚拟机的支持吗? 至少需要哪几个条件小程序才能运行......
- 请问怎样设置web应用程序部署文件启动server端socket监听程序,并且web服务能够正常启动
- Java 8 新特性 泛型,语法以及相关
- nohup后台运行的程序运行一段时间以后会突然中断
- 何时分配内存???
- 请问字段是像我这样封装的么?
还是a和b都有6 ?
如果简单一点的话,可以用removeAll, retainAll方法,看看原来那个数组是不是没有改变,或者清空了
2. using for loops to detect is the most simple way...
或者试下用用arrays的sort方法先对b.c排下序,在用binarySearch(type[] a, type key)方法看看a中的元素是否在b.c中能否找到。
如果HashSet地size等于数据的长度就没有重复,如果不等于就有重复
多维数组可以把每维数组的长度加起来
public static void main(String[] args) {
int[][] a = { {1,2,3,4,5,6},
{3,45,5,6,7,44}} ;
int[][] b = {{33,56,77,9,10,80} ,
{11,8,9,45,88,90}};
System.out.println(bijiao(a,b));
}
public static boolean bijiao(int x[][],int y[][]) {
boolean z = true;
for(int i=0;i<2;i++) {
for(int j=0;j<6;j++) {
for(int a=0;a<2;a++) {
for(int b=0;b<6;b++) {
if(x[i][j] == y[a][b]) {
z = false;
break;
}
}
}
}
}
return z;
}
}
你可以看看代码或者可以自己写将一个数组里的数据与另一个进行循环比较
呵呵
重新声明问题哦。
a二维数组中有
{1,2,3,4,5,6}
{3,45,5,6,7,44}
b二维数组中有
{1,2,3,4,5,6}
{6,8,9,45,88,90}
c二维数组中有
{122,55,67,99,80,199}
{777,595,97,234,898,102}
a跟b有相同的一个元素{1,2,3,4,5,6}所以要返回false
a跟c无相同的元素 返回true
对不起,误导了!
呵呵
回答给150分。
public static void main(String[] args) {
int[][] a = { {1,2,3,4,5,6},
{3,45,5,6,7,44}} ;
int[][] b = {{1,2,3,4,5,6} ,
{3,8,9,45,88,90}};
int[][] c = {{122,55,67,99,80,199} ,
{777,595,97,234,898,102} };
System.out.println(bijiao(a,b));
System.out.println(bijiao(a,c));
}
public static boolean bijiao(int x[][],int y[][]) {
boolean z = true;
for(int i=0;i<2;i++) {
int t = 0;
for(int j=0;j<6;j++) {
for(int a=0;a<2;a++) {
if(x[i][j] == y[a][j]) {
t++;
if(t == 6) {
z = false;
break;
}
}
}
}
}
return z;
}
}
/**
*
*/
package houlei.test;import java.util.Arrays;/**
* 该类创建于 2008-8-29 下午04:19:04
*
* @version 1.0.0
* @author 侯磊
*/
public class E { public static void main(String[] args) {
int a[][] = new int[][] { { 1, 2, 3, 4, 5, 6 }, { 3, 45, 5, 6, 7, 44 } };
int b[][] = new int[][] { { 33, 56, 77, 9, 10, 80 }, { 6, 8, 9, 45, 88, 90 } };
int c[][] = new int[][] { { 122, 55, 67, 99, 80, 199 }, { 777, 595, 97, 234, 898, 102 } };
System.out.println(dissimilitude(a, b));
System.out.println(dissimilitude(a, c));
} public static boolean dissimilitude(int a[][], int b[][]) {
int ta[] = sort(a);
int tb[] = sort(b);
int i = 0, j = 0;
while (i < ta.length && j < tb.length) {
if (ta[i] == tb[j])
return false;
else if (ta[i] < tb[j])
i++;
else
j++;
}
return true;
} public static int[] sort(int a[][]) {
int t[] = new int[a.length * a[0].length];
int index = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++) {
t[index++] = a[i][j];
}
Arrays.sort(t);
return t;
}
}
{
public static void main(String str[])
{
int a[][]=new int[][];
int b[][]=new int[][];
find(a,b);
find(b,a);
}
private void find(int a[][],int b[][])
{
for(int i=0;i<a.length;i++)
{ for(int j=0;j<a[i].length;j++)
if(findcode(a,b,i,j));
if(array1.equals(array2))
{System.out.println(" a include b Exit ");System.exit(0);//a包含b退出系统
else
{System.out.println(" b include a Exit ");System.exit(0);//b包含a退出系统}
}//find end
private boolean findcode(int src[][],int tar[][],int row,int col)
{
for(int i=0;i<tar.length;i++)
{
for(int j=0;j<tar[i].length;j++)
if(src[row][col]== tar[i][j])
{return true;}//包含元素
else
continue;
return false;//直到结束也不包含
}}//findcode end
}//clsss end
public static void main(String args[]){
int a[][] = new int[][] { { 1, 2, 3, 4, 5, 6 }, { 3, 45, 5, 6, 7, 44 } };
int b[][] = new int[][] { { 33, 56, 77, 9, 10, 80 }, { 6, 8, 9, 45, 88, 90 } };
System.out.print(detect(a, b));
}
public static boolean detect(int[][] a, int[][] b){
int sizeFirst, sizeSecond, total;
Set<Integer> setForArrayFirst = new TreeSet<Integer>();
Set<Integer> setForArraySecond = new TreeSet<Integer>();
for (int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++)
setForArrayFirst.add(new Integer(a[i][j]));
}
for (int i=0; i<b.length; i++){
for(int j=0; j<b[i].length; j++)
setForArraySecond.add(new Integer(b[i][j]));
}
sizeFirst = setForArrayFirst.size();
sizeSecond = setForArraySecond.size();
total = sizeFirst + sizeSecond;
Iterator iterator = setForArraySecond.iterator();
while(iterator.hasNext())
setForArrayFirst.add((Integer)iterator.next());
if (setForArrayFirst.size() < total){
return false;
}
return true;
}
}
import java.util.HashSet;public class Test10 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = {
{1, 2, 3, 4, 5, 6},
{3, 45, 5, 6, 7, 44}
};
int[][] b = {
{33, 56, 77, 9, 10, 80},
{6, 8, 9,45, 88, 90}
};
int[][] c = {
{122, 55, 67, 99, 80, 199},
{777, 595, 97, 234, 898, 102}
}; if (isContained(a, b)) {
System.out.println("The array B contains some same elements with A");
} else {
System.out.println("The array B does not contain some same elements with A");
}
if (isContained(a, c)) {
System.out.println("The array C contains some same elements with A");
} else {
System.out.println("The array C does not contain some same elements with A");
}
}
public static boolean isContained(int[][] a, int[][] b) {
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
set.add(a[i][j]);
}
}
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
if (set.contains((Integer)b[i][j])) {
return true;
}
}
}
return false;
}}
运行结果:
The array B contains some same elements with A
The array C does not contain some same elements with A
所以大家最好做成接口形式。我给些我做的给大家看看,
不过很乱。
大家不要见外
import java.util.*;
class rule {
public Random r = new Random(); public rule() { } public int[] rule1(int[] array) {
int i = 0;
while (i < 8) {
array[i] = rule2();
i++;
}
return array;
} public int rule2() {
int F = r.nextInt(34);
if (F == 0) {
F = r.nextInt(34);
}
return F;
} public int[] rule3(int[] array) {
int t = 0;
for (t = 0; t < array.length; t++) {
for (int j = t + 1; j < array.length; j++) {
if (array[t] == array[j]) {
array[j] = rule2();
}
}
}
return array;
} public int[] rule4(int[] array) {
int i, j;
int temp;
for (i = 0; i < array.length; i++) {
for (j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}}public class ciaopiao {
public ciaopiao() {
//得到8个数的数组。
List<Integer> list8=Get8From33();
System.out.println(list8);
//for test
int MKJ[]=new int[8];
//产生56个组合。
for (int i=0;i<8;i++){
for (int j=i+1;j<8;j++)
{
List<Integer> list6=new ArrayList<Integer>();
for (int m=0;m<8;m++)
if (m!=i&&m!=j)
list6.add(list8.get(m));
Collections.sort(list6);
//已经产生了一个6个数的数组。
System.out.println(list6);
}
}
}
//交换List里,指定索引的两个元素。
void Swip(List<Integer> list,int m,int n)
{
Integer temp = list.get(m);
list.set(m,list.get(n));
list.set(n,temp);
}
//从33个数中得到8个数。
/* List<Integer> Get8From33() {
List<Integer> list33 = new ArrayList<Integer>();
for(int i=0;i<33;i++)
list33.add(i+1);
//随机的交换list33中的元素若干次。
for (int i=0;i<100;i++){
int m=(int)(Math.random()*list33.size());
int n=(int)(Math.random()*list33.size());
Swip(list33,m,n);
}
return list33.subList(0,8);
}
*/
//***************任选8个红球作为一组*************************
public List<Integer> Get8From33(){
List<Integer> listT=new ArrayList<Integer>();
rule mkj=new rule();
int array[]=new int[8];
mkj.rule4(mkj.rule3(mkj.rule1(array)));
for(int i=0;i<8;i++)
{
listT.add(array[i]);
}
return listT;
}
public static void main(String[] args) {
new ciaopiao();
}
boolean equals(Object o)比较指定对象与此 set 的相等性。如果指定的对象也是一个 set,两个 set 的大小相同,并且指定 set 的所有成员都包含在此 set 中(或者,此 set 的所有成员都包含在指定的 set 中也一样),则返回 true。此定义确保了 equals 方法可在不同的 set 接口实现间正常工作。 这是api里的解释 注意 我觉得这样不能判断contains some same elements
public static void main(String[] args)
{
int[][] a = new int[][]
{
{ 1, 2, 3, 4, 5, 6 },
{ 3, 45, 5, 6, 7, 44 } };
int[][] b = new int[][]
{
{ 1, 2, 3, 4, 5, 6 },
{ 6, 8, 9, 45, 88, 90 } };
Test t = new Test();
System.out.println(t.check(a, b));
}
public boolean check(int a[][],int b[][])
{
String str = Arrays.toString(a[0]);
String str2 = Arrays.toString(a[1]);
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
if(Arrays.toString(a[i]).equals(Arrays.toString(b[j])))
{
return false;
}
}
return true;
}}
{
int[][] a = new int[][]
{
{ 1, 2, 3, 4, 5, 6 },
{ 3, 45, 5, 6, 7, 44 } };
int[][] b = new int[][]
{
{ 1, 2, 3, 4, 5, 6 },
{ 6, 8, 9, 45, 88, 90 } };
Test t = new Test();
System.out.println(t.check(a, b));
}
public boolean check(int a[][],int b[][])
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
if(Arrays.toString(a[i]).equals(Arrays.toString(b[j])))
{
return false;
}
}
return true;
}}