---------------------------------------------------------

ArrayList<String> arrayList = new ArrayList<String>(10);
System.out.println(arrayList.size());

大家猜,打印结果会是什么?会是10吗?

答案:0

因为,new ArrayList<String>(10),只是告诉jvm分配10个Stirng的空间出来,arrayList 不是null而已。此时的arrayList 无任何内容!

---------------------------------------------------------

为什么说JDK中处理String的indexOf()方法效率低,建议不要使用?  今天探了个究竟。

上源码!

static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
} char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount); for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
} /* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++
); if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}

真整齐啊!

上来就看到了两个for循环。问题必然出在这里!和KMP算法不同,这明显是简单粗暴的loop index。为什么没采用复杂度为O(m+n)的KMP算法?估计时间问题吧。

以后注意尽量避免indexOf()方法。

顺便窥探了下subString()方法的实现:

上源码!

public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
} public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}

怪不得效率较高,原来用到了Arrays.copyOfRange()方法。

---------------------------------------------------------

一道非常老的面试题:

从字符串str1中找出子字符串str2出现的次数。比如:

String str1 = "sgiccomcocmcomomameadjcomfjecomcomcomcijr";
String str2 = "com";

自己想到的方法如下:

//暴力匹配
//KMP算法
//subString
//split
//RegEx(may be the best)

这其中,RegEx(正则匹配)的效率是最高的(个人理解)。而且实现起来也很简单,如下:

String str1 = "sgiccomcocmcomomameadjcomfjecomcomcomcijr";
String str2 = "com";
int times = 0;
Pattern pattern = Pattern.compile(str2);
Matcher matcher = pattern.matcher(str1);
while (matcher.find()) {
times++;
System.out.println(matcher.group());
}
System.out.println(times);

---------------------------------------------------------

7/30

刚刚正在探索KMP算法,然后有经验的同事过来看看究竟。不过扯出了另外一个话题,正则的效率不一定高!用他的话说,java代码需要JVM进行解释,那正则表达式也是需要解释器的(可以这么理解),所以效率很可能会低的。

另外,String类中也存在matches()方法,遂对该方法和Pattern中的matches()进行了对比。

代码如下:

public static void main(String[] args){
String string = "ddenfj#@fe_dw.comw";
int count = 100000;
long before = System.currentTimeMillis();
Runtime runtime = Runtime.getRuntime();
long r1 = runtime.totalMemory();
for (int i = 0; i < count; i++) {
string.matches("^([\\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$");
}
long r2 = runtime.totalMemory();
long end = System.currentTimeMillis();
System.out.println("the memory of match() in String is = " + (r2-r1)/1024);
System.out.println("the time of match() in String is = " + (end-before)); //Pattern
Pattern pattern = Pattern.compile("^([\\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$");
before = System.currentTimeMillis();
r1 = runtime.totalMemory();
for (int i = 0; i < count; i++) {
pattern.matcher(string).matches();
}
r2 = runtime.totalMemory();
end = System.currentTimeMillis();
System.out.println("the memory of match() in Pattern is = " + (r2-r1)/1024);
System.out.println("the time of match() in Pattern is = " + (end-before));
}

在count=100000的情况下,发现结果如下:

the memory of match() in String is = 92416
the time of match() in String is = 327
the memory of match() in Pattern is = 0
the time of match() in Pattern is = 26

结果让我吃惊!String类中的matches()方法相对于Pattern类中的matches()方法,简直不能用!

究其原因:String类中的matches()方法调用了Pattern.matches(regex, this),而该方法的实现如下:

public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}

强调部分的重复执行会占用大量内存。

而且该方法的解释中:If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.

总之,String类中的matches()方法不要使用!

---------------------------------------------------------

今天同事问了个问题:不同版本的jar包,能同时存在吗?如果能的话,应该加载的哪个?

做了个实验,把poi-3.10-beta2.jar和poi-3.10-FINAL.jar(我把名称改为chris.jar)放在lib文件夹下,编译没错,证明可以共存。

但是使用哪个jar包呢?最新的。怎么区分最新的?根据版本号?

最后证明编译器仍然使用chris.jar下的class文件。

结论:当不同版本的jar包共存时,编译器将根据class的时间以及META-INF下的信息区分哪个jar包是最新的,并加载之。

---------------------------------------------------------

 

java乱炖的更多相关文章

  1. HTM CSS 笔记乱炖

    一.常用实体(字符转义) '<' == '<' '©' == '©' '>' == '>' '"' == '"' ' ' == ' ' '®' == '®' ...

  2. Jerry的ABAP, Java和JavaScript乱炖

    写这个系列的初衷是SAP Chengdu office有越来越多的应届毕业生加入,这些新同事通过在大学的专业学习,具备了Java和JavaScript背景,但是进入SAP之后大家觉得ABAP没有Jav ...

  3. Anliven - 乱炖

    001 --- Ping Yourself! 由TCP/IP协议栈而想到的: 你的"协议分层"是如何的?有谁或者什么事务所对应着?谁先谁后,什么重要? 你的"协议栈&qu ...

  4. [PHP知识点乱炖]四、全局变量——小偷从良记

    本章要讲的是PHP的全局变量. 这里讲个小故事: 很多年前,一个很聪明的小偷,想去偷一户人家的钱.可是他偷不到主人的钥匙,怎么办呢? 他想到了一个办法,去之前嚼了一块口香糖,口香糖的牌子是“大大泡泡糖 ...

  5. VAO VBO IBO大乱炖

    最近对程序中绘制卡顿的问题忍无可忍,终于决定下手处理了.程序涉及的绘制比较多,除了点.线.三角形.多边形.圆柱体之外,还有自组格式模型.开始想全部采用显示列表优化,毕竟效率最高,虽然显示列表存在编译之 ...

  6. Git 仓库 SSH、HTTP、Gitweb (Nginx) 乱炖

    简介: 自己搭建 Git 仓库,实现 SSH 协议.配合 Nginx 实现 HTTP 协议拉取.推送代码. 利用 Nginx 实现 Gitweb 在线浏览代码,使用 Gitweb-theme 更新默认 ...

  7. js知识点乱炖

    修改属性 元素.style.样式=值     document.getElementById('box').style.width='200px'; 属性操作方式 1.. 的 元素.属性名如果属性是单 ...

  8. HTML精确定位之位置参数乱炖一锅

    一.前言 公司项目,需要在一个图片的右上角放置一个类似“X”的东西(其实是需要显示一个数字,就像微信一样,在右上角显示几个消息),然后需要用到html的定位,看了几个网上的例子,恍惚间看到了一个off ...

  9. Shiro乱炖

    眼瞅着7月份又要浑浑噩噩的荒度过去了... 说好的计划呢?人的惰性真是无法治愈的伤痛啊 话说最近研究Shiro, Linux和JavaScript, 但结果不怎么如意:Shiro还停留在理解拦截器方面 ...

随机推荐

  1. 【01】markdown特殊说明

    [01]说明 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令 ...

  2. php官方微信接口大全

    微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中.内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <?php class ...

  3. Non-maximum suppression(非极大值抑制算法)

    在RCNN系列目标检测中,有一个重要的算法,用于消除一些冗余的bounding box,这就是non-maximum suppression算法. 这里有一篇博客写的挺好的: http://www.c ...

  4. 组合数学的卡特兰数 TOJ 3551: Game of Connections

    这个就是卡特兰数的经典问题 直接用这个公式就好了,但是这个题涉及大数的处理h(n)=h(n-1)*(4*n-2)/(n+1) 其实见过好几次大数的处理了,有一次他存的恰好不多于30位,直接分成两部分l ...

  5. hdu 1848 sg——dfs&&打表双实现

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. LogMiner配置使用手册

    LogMiner配置使用手册 1 Logminer简介 1.1 LogMiner介绍 Oracle LogMiner 是Oracle公司从产品8i以后提供的一个实际非常有用的分析工具,使用该工具可以轻 ...

  7. haskell 乱搞(2)之 Y-conbinator [原创]

    Y-conbinator"有没有用"?并没有,在大多数支持函数式编程的语言里,你可以自由的使用递归,而这货只是作为理论基石弥散在函数式编程的血肉之中 这是数学笔记,这是数学笔记,这 ...

  8. P1140 相似基因 (动态规划)

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  9. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

  10. 路飞学城详细步骤 part1

    详细步骤 1 添加登录页面 步骤: Header.vue 写一个登录按钮,<router-link to = ' /xx'> 在路由的 index.js中添加这个 新的路由,{'path' ...