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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 侯凯斌 中级黑马   /  2012-11-20 14:16  /  1664 人查看  /  7 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

1.有2个数组A,B. B数组中元素包含在A数组中,请写一段代码把A数组中B没有的元素放到C数组中。

2.加入数组中都是数字,而且已经按大小排序,请写一段代码以最快效率把上一题的元素放到C数组中。

7 个回复

倒序浏览
public class CopyArray {   
    public static void main(String[] args) {   
        copyNoSort();   
        System.out.println("sort: ");   
        copySort();   
    }   

        // 循环进行m*n次   
    public static void copyNoSort() {   
  
        int[] arrayA = new int[] { 11, 1, 2, 13, 4, 5, 6, 7, 8, 9 };   
        int[] arrayB = new int[] { 2, 4, 6, 8 };   
  
        int[] arrayC = new int[arrayA.length - arrayB.length];   
        int t = 0;   
        for (int i = 0; i < arrayA.length; i++) {   
            boolean isHave = true;   
            for (int k = 0; k < arrayB.length; k++) {   
                if (arrayA[i] == arrayB[k]) {   
                    isHave = false;   
                }   
            }   
  
            if (isHave) {   
                arrayC[t] = arrayA[i];   
                t++;   
            }   
        }   
  
        for (int i = 0; i < arrayC.length; i++) {   
            System.out.print(arrayC[i] + ", ");   
        }   
  
    }   
  

    public static void copySort() {   
        int[] arrayA = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13 };   
        int[] arrayB = new int[] { 2, 4, 6, 8 };   
  
        int[] arrayC = new int[arrayA.length - arrayB.length];   
  
        int k = 0;   
        int t = 0;   
        for (int i = 0; i < arrayA.length; i++) {   
  
            if (k < arrayB.length) {   
                if (arrayA[i] < arrayB[k]) {   
                    arrayC[t] = arrayA[i];   
                    t++;   
                } else {   
                    k++;   
                }   
            } else {   
  
                arrayC[t] = arrayA[i];   
                t++;   
            }   
        }   
  
        for (int i = 0; i < arrayC.length; i++) {   
            System.out.print(arrayC[i] + ", ");   
        }   
    }   
}  

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
看看吧...
回复 使用道具 举报
聽聽我dē❤ 发表于 2012-11-20 14:28
public class CopyArray {   
    public static void main(String[] args) {   
        copyNoSort();    ...

第二题方法是不对的。
int[] arrayC = new int[arrayA.length - arrayB.length];特殊情况下(A.length < B.length),这句会报错越界;
eg:
   int[] arrayB = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13 };  
   int[] arrayA = new int[] { 6, 8 , 14, 16 , 18, 19, 20};  
回复 使用道具 举报
将a数组元素全都扔到list集合中,然后删除b数组中存在的元素。剩下的元素再放进c数组中。长度为a数组长度与b数组长度之差。
回复 使用道具 举报
森仔 发表于 2012-11-20 15:05
第二题方法是不对的。
int[] arrayC = new int[arrayA.length - arrayB.length];特殊情况下(A.length <  ...

B数组中元素包含在A数组中
所以这是不会出现越界现象的,也不会出现B数组中的数多于A数组的,你看清题意在说吧。
回复 使用道具 举报
森仔 中级黑马 2012-11-20 18:16:12
7#
聽聽我dē❤ 发表于 2012-11-20 17:21
B数组中元素包含在A数组中
所以这是不会出现越界现象的,也不会出现B数组中的数多于A数组的,你看清题意 ...

;P看错了
回复 使用道具 举报
黑马吕世成 来自手机 中级黑马 2012-11-21 17:56:28
8#
聽聽我dē❤ 发表于 2012-11-20 14:28:43
public class CopyArray {   
    public static void main(String[] args) {   
        copyNoSort();

可以先算出c数组的长度,只要再后面判断数组c是否装满,满了就直接退出循环。这样可以减少平均比较的次数。来自: Android客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马