不良代码展示-两个数组找不同

原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6912428

不良代码:

public class WrongCompare { /** * @param args the command line arguments */ public static void main(String[] args) { String[] str1 = {"1","2","3","4","5","6",}; String[] str2 = {"0","2","3","4","5","6","7",}; for(int i = 0; i < str1.length; i++) { boolean has = false; for(int j = 0; j < str2.length; j++) { if (str1[i].equals(str2[j])) { has = true; break; } } if (!has) { System.out.println(str1[i] + " 在str1中,不在str2中"); } } for(int j = 0; j < str2.length; j++) { boolean has = false; for(int i = 0; i < str1.length; i++) { if (str1[i].equals(str2[j])) { has = true; break; } } if (!has) { System.out.println(str2[j] + " 在str2中,不在str1中"); } } } }


比较两个数组的不同,竟然用了2次双层循环去做判断。这个开销是很大的。
有时候,我们会发现自己写的程序,调试的时候没什么问题,一旦在真实环境下就慢的受不了。

平时写代码的时候,就要注意技巧。


可以用的方法:

public class RightCompare { /** * @param args the command line arguments */ public static void main(String[] args) { String[] str1 = {"1","2","3","4","5","6",}; String[] str2 = {"0","2","3","4","5","6","7",}; Set set1 = new HashSet(); Set set2 = new HashSet(); set1.addAll(Arrays.asList(str1)); set2.addAll(Arrays.asList(str2)); Iterator iter = set1.iterator(); while(iter.hasNext()) { Object o = iter.next(); if (set2.contains(o)) { set2.remove(o); } else { System.out.println(o + " 在str1中,不在str2中"); } } iter = set2.iterator(); while(iter.hasNext()) { Object o = iter.next(); System.out.println(o + " 在str2中,不在str1中"); } } }
用Set的方法。set不是用列表方式去存放数据,无序的存放,在效率上会更高一些。

Hashset用的是哈希散列算法去存放数据,判断数据是否在集合内,开销比列表里的判断要快不少,特别是大数据集的时候。

在手机开发的时候,这种效率上的提升,会更明显。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 要找出两个数组中不同的数,可以使用以下方法。首先,将两个数组分别存储在nums1和nums2中。然后,创建一个空的列表answer,用于存储不同的数。接下来,使用for循环遍历nums1中的数,判断是否在nums2中,如果不在,则将其添加到answer\[0\]中。然后,再使用for循环遍历nums2中的数,判断是否在nums1中,如果不在,则将其添加到answer\[1\]中。为了避免重复的数出现在answer中,可以使用set函数来去除重复的数。最后,输出answer即可得到不同的数。\[1\] 示例代码如下: ``` nums1 = \[1, 2, 3, 4, 5, 6, 7\] nums2 = \[1, 2, 3, 4\] answer = \[\[\], \[\]\] for i in set(nums1): if i not in nums2: answer\[0\].append(i) for j in set(nums2): if j not in nums1: answer\[1\].append(j) print(answer) ``` 输出结果为:\[\[5, 6, 7\], \[\]\],表示nums1中的不同数为\[5, 6, 7\],nums2中的不同数为空列表。\[2\] 另外,还可以使用其他方法实现,比如使用filter函数和indexOf函数来筛选不同的数。具体代码如下: ``` nums1 = \[1, 2, 3, 4, 5, 6, 7\] nums2 = \[1, 2, 3, 4\] res1 = \[\] res2 = \[\] res = \[\] res1.append(list(filter(lambda x: x not in nums2, nums1))) res2.append(list(filter(lambda x: x not in nums1, nums2))) res = res1\[0\] + res2\[0\] print(res) ``` 输出结果为:\[5, 6, 7\],与之前的方法得到的结果相同。\[3\] #### 引用[.reference_title] - *1* *2* [找出两数组的不同](https://blog.csdn.net/gschen_cn/article/details/124010765)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [比较两个数组,找出不一样的数据组成新数组](https://blog.csdn.net/qq_37600506/article/details/122602819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值