算法总结

来源:互联网 发布:福建广电网络爱家app 编辑:程序博客网 时间:2024/05/20 13:19

对于数组查找:

1、for循环,遍历

2、构建hashmap, 直接查找

Map<int,int> map=new HashMap<int,int>();

map.put(array[i],i)

map.containsKey(temp)

notice:此时如果数组当中有相同的元素,那么对hashmap来说,就会只有一个key值。


对于数组,只要对下标做引用,一定要无时无刻防止溢出,

如String[] strs;

strs[0]---->判断strs是否为空,或者strs.length==0

strs[j].charAt(i)-----> 判断i在不在strs[j]的长度之内 

strs[0].length()----> 判断strs[0]是否为空

需要注意的是  

        String a=""; //a.length=0        String b=null;//b.length 报错java.lang.NullPointerException        String[] c=null;//c.length 报错java.lang.NullPointerException

另外,对于字符串,比较值的大小,尽量用equals;对于char型的,用==


原创粉丝点击