如何判断某个值是否在数组中存在?
有数据数组a[]={11,22},b[]={11,33,44},如果a中的其中一个值在b中存在,则打印出1;如果a中的任何一值都不存在于b中,则打印出2;如果a中的值与数目都与b一样,则打印出3;如何用程序实现啊?
------解决方案--------------------接分啦。。
package jsp;
import java.util.Arrays;
import java.util.List;
public class test {
public static void main(String[] args) {
int a[] = {11, 22}, b[] = {11, 33, 44};
List list = Arrays.asList(a);
List list2 = Arrays.asList(b);
for (int i = 0; i < list.size(); i++) {
if (list2.contains(list.get(i))) {
System.out.println( "1 ");
break;
}
}
if (!list2.containsAll(list)) {
System.out.println( "2 ");
}
if (Arrays.equals(a, b)) {
System.out.println( "3 ");
}
}
}
------解决方案--------------------我市新手 写的代码有点混乱~~希望指教~~
//:Main.java
class Than{
private int c=0;
private int[] a;
private int[] b;
Than(int[] a,int[] b){
this.a=a;
this.b=b;
}
void sort(){
for(int i=0;i <a.length;i++){
for(int j=0;j <b.length;j++){
if(a[i]==b[j]){
c++;
}
}
}
}
void ending(){
if(c==0){
System.out.println( "2 ");
}
if(c==1){
System.out.println( "1 ");
}
if(a.length==b.length&c==b.length){
System.out.println( "3 ");
}
}
}
public class Main{
public static void main(String[] args){
int[] a={11,22};
int[] b={11,33,44};
Than th=new Than(a,b);
th.sort();
th.ending();
}
}
------解决方案--------------------class Than{
private int c=0;
private int[] a;
private int[] b;
Than(int[] a,int[] b){
this.a=a;
this.b=b;
}
void sort(){
for(int i=0;i <a.length;i++){
for(int j=0;j <b.length;j++){
if(a[i]==b[j]){
c++;
}
}
}
}
void ending(){
if(a.length==b.length&c==b.length){
System.out.println( "3 ");
}
else if(c==0){
System.out.println( "2 ");
}
else if(c==1){
System.out.println( "1 ");
}
}
}
public class test{
public static void main(String[] args){
int[] a={11,22};
int[] b={11,33,44};
Than th=new Than(a,b);
th.sort();
th.ending();
}
}