A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

定义两个数组int[] a = new int[]{aa,bb,cc,dd,ff}
                  int[] b = new int[]{bb,ff}
用循环判断的方法,当b的元素都包含在a中时,自动跳出。
这道题求解。

15 个回复

倒序浏览
数组里面是字符串吧
回复 使用道具 举报
这是什么数组,声明数组里面的元素类型是int,但放进去的有不是int 类型,这是什么情况
回复 使用道具 举报
光sail 发表于 2012-4-24 22:23
数组里面是字符串吧

对的,是字符串。我忘记写上了。
回复 使用道具 举报
前面的int是我随便写的。题目是定义一个字符串型数组,“aa”....“ff”然后b等于"bb
","ff"下面的条件就是这样。
回复 使用道具 举报
我的思路,已经知道是a包含b,遍历数组b,每个元素查看是否在 a 中。有一个不在马上返回 false,全部都在,返回TUER,跳出
回复 使用道具 举报
问题不对吧,int数组存放aa,bb,cc,dd?
回复 使用道具 举报
public class Test {
                public static void main(String[] args) {
                        String[] a = new String[]{"aa","bb","cc","dd","ff"};
                        String[] b = new String[]{"bb","ff"};
                        boolean n=true;
                        int y=0;
                        while (n){
                                for (int i=0;i<b.length;i++){
                                        String str=b[i];
                                        for(int t=0;t<a.length;t++){
                                                String str1=a[t];
                                                if (str.equals(str1)){
                                                        y++;
                                                }
                                                if(y==i){
                                                       
                                                        n=false;
                                                }
                                        }
                                }
                        }
                        System.out.println(y);

}
}
打印输出y=2,因为数组b的两个元素在数组a中找到
回复 使用道具 举报
本帖最后由 欧阳梦南 于 2012-4-24 22:44 编辑


public class oy {

        public static void main(String[] args) {


                String [] a = new String[]{"aa","bb","cc","dd","ff"};
        String [] b = new String[]{"bb","cc","ac"};
        int count=0;
        for(int i=0;i<b.length;i++)
        {
                for(int j=0;j<a.length;j++)
                {
                        if(b.equals(a[j])   )
                                count++;
                        
                                
                }
                if(count!=(i+1))
                {
                        System.out.println("有不包含的元素");
                        //break;
                }
                        
        }

               
               

        }

}


是说这个意思吧?
但这个每个数组不能有重复的元素
回复 使用道具 举报
付左军 发表于 2012-4-24 22:30
我的思路,已经知道是a包含b,遍历数组b,每个元素查看是否在 a 中。有一个不在马上返回 false,全部都在, ...

马上返回false? 什么 意思
回复 使用道具 举报
if(b.equals(a[j])   )
我那个b后面有个中括号,里面是 i  。不知道为嘛,粘贴过来老是少一些中括号。还修改不过来
回复 使用道具 举报
class  Sub
{
        public static void main(String[] args)
        {
                System.out.println("Hello World!");
                new A().aa();
        }
}
class A
{
        public void aa()
        {
                int x=0,i=0,j=0;
                String[] a = new String[]{"aa","bb","cc","dd","ff"};
                String[] b = new String[]{"bb","ff"};
               
                        for(i=0;i<b.length;i++){
                                for(j=0;j<a.length;j++)
                                {

                                        if(b[i].equals(a[j]))
                                        {
                                                x++;
                                        }
//                                        System.out.println(x);
                                        if(x==b.length){
                                                System.out.println("b的元素都包含在a中,自动跳出");
                                                return;
                                        }
                                }
                        }
                        System.out.println("b的元素不都包含在a中");
        }
}应该是这样的吧
回复 使用道具 举报
褚代江 发表于 2012-4-24 23:18
class  Sub
{
        public static void main(String[] args)

为什么运行不出结果。编译没问题。运行报错了。
回复 使用道具 举报
崔瑞龙 发表于 2012-4-24 23:31
为什么运行不出结果。编译没问题。运行报错了。

没呀。我写了后运行可以的啊。你运行到哪错了啊,
回复 使用道具 举报
String[] a=new String[]{"aa","bb","cc","dd","ff"};
                String[] b=new String[]{"bb","ff"};
                int count=0;
                for(int ib=0;ib<b.length;ib++){
                        for(int ia=0;ia<a.length;ia++){
                                if(b[ib].equals(a[ia])){
                                        count++;
                                        break;//中断此次循环避免重复匹配
                                }
                        }
                }
                if(count==b.length){
                        System.out.println("b数组中元素都在a数组中");
                        return;
                }

跟上面几位同学的解题思路类似,不过我觉得总归是要走完全部循环的不如把退出判断放在外面的好
回复 使用道具 举报
String[] a = new String[]{"aa","bb","cc","dd","ff"};
          String[] b = new String[]{"bb","ee"};
          int y=0;
          int bb=b.length;
          for(int i=0;i<bb;i++){
                  for(int j=0;j<a.length;j++){
                          if(b[i].equals(a[j])){
                                  y++;
                          }
                  }
          }
          if(y==bb){
                  System.out.println("b在数组a中,结束循环");
                  return;
          }else{
                  System.out.println("b不在数组a中,结束循环");
          }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马