摘要:今天瀏覽了一下里的類,發(fā)現(xiàn)一個(gè)靜態(tài)方法有點(diǎn)意思,就是我們常用的的底層實(shí)現(xiàn),先看下代碼調(diào)用鏈。所以字符串的長度是可以不用匹配的,故是沒問題的。關(guān)鍵的地方是這里加上了,是字符串的起始匹配偏移量,即從的哪個(gè)字符開始匹配。
今天瀏覽了一下java里的String類,發(fā)現(xiàn)一個(gè)靜態(tài)方法有點(diǎn)意思,就是我們常用的indexOf(String str)的底層實(shí)現(xiàn),先看下代碼調(diào)用鏈。
public int indexOf(String str) { return indexOf(str, 0); } public int indexOf(String str, int fromIndex) { return indexOf(value, 0, value.length, str.value, 0, str.value.length, fromIndex); } static int indexOf(char[] source, int sourceOffset, int sourceCount, String target, int fromIndex) { return indexOf(source, sourceOffset, sourceCount, target.value, 0, target.value.length, fromIndex); } /** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * @param sourceOffset offset of the source string. * @param sourceCount count of the source string. * @param target the characters being searched for. * @param targetOffset offset of the target string. * @param targetCount count of the target string. * @param fromIndex the index to begin searching from. */ 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; }
底層的字符串匹配的邏輯比較簡單,就是普通的匹配模式:
查找首字符,匹配target的第一個(gè)字符在source內(nèi)的位置,若查找到max位置還找到,則返回-1;
若在source匹配到了target的第一個(gè)字符,那么在依次比較srouce和target后面的字符,一直到target的末尾;
如果target后面的字符與source都已經(jīng)匹配,則返回在source上匹配到的第一個(gè)字符的相對下標(biāo),否則返回-1。
但是仔細(xì)讀代碼會(huì)發(fā)現(xiàn)一個(gè)問題,就是這里
int max = sourceOffset + (sourceCount - targetCount);
max的計(jì)算方式,max的作用是計(jì)算出最大的首字符匹配次數(shù),取值范圍應(yīng)該是"max <= sourceCount"。
所以target字符串的長度是可以不用匹配的,故“sourceCount - targetCount”是沒問題的。
關(guān)鍵的地方是這里加上了sourceOffset,sourceOffset是source字符串的起始匹配偏移量,即從source的哪個(gè)字符開始匹配。
所以,根據(jù)代碼里的max計(jì)算方式,最終計(jì)算出來的max值是會(huì)有可能大于sourceCount。
看下測試代碼:
package string; /** * string test */ public class StringTest { 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; } public static void main(String[] args) { String source = "abcdefghigklmn"; String target = "n"; int sourceOffset = 5; int targetOffset = 0; int index = indexOf(source.toCharArray(), sourceOffset, source.length(), target.toCharArray(), targetOffset, target.length(), 0); System.out.println(index); } }
如果target在source內(nèi)可以匹配到返回正確結(jié)果8(結(jié)果8是相對于sourceOffset的結(jié)果,如果轉(zhuǎn)換成source內(nèi)的位置則是13)。
但是如果target在source內(nèi)匹配不到,則會(huì)拋出java.lang.ArrayIndexOutOfBoundsException異常,如下:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14 at string.StringTest.indexOf(StringTest.java:27) at string.StringTest.main(StringTest.java:52)
可見報(bào)出越界的下標(biāo)是14,這就是由于max = sourceOffset + (sourceCount - targetCount)引起,計(jì)算出的max值為:17。
所以,個(gè)人認(rèn)為max計(jì)算這里是個(gè)潛在的BUG,應(yīng)該改為 int max = sourceCount - targetCount;
不過這個(gè)方法是一個(gè)非public方法,只在String內(nèi)部調(diào)用,同時(shí)也跟蹤了所有對該方法的調(diào)用鏈,都是傳入的默認(rèn)0,在使用時(shí)不會(huì)出現(xiàn)數(shù)組越界問題。
不知這是開發(fā)者故意為之,還是其它我未知用意,歡迎大家交流討論?。?!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72701.html
摘要:但是這并不妨礙我們從思維拓展的角度出發(fā),看看去重可以用幾種思路去實(shí)現(xiàn)。首先是常規(guī)的雙層循環(huán)比對的思路實(shí)現(xiàn)定義一個(gè)變量表示當(dāng)前元素在中是否存在。依次對中的元素和原數(shù)組元素進(jìn)行比對。重點(diǎn)是保證碰撞的幾率小到比中大獎(jiǎng)還小就可以了。 前端在日常開發(fā)中或多或少都會(huì)碰到有對數(shù)據(jù)去重的需求,實(shí)際上,像是lodash這些工具庫已經(jīng)有成熟完備的實(shí)現(xiàn),并且可以成熟地運(yùn)用于生產(chǎn)環(huán)境。但是這并不妨礙我們從思維...
摘要:昨天在微博上看到一篇文章,也寫數(shù)組去重,主要推崇的方法是將利用數(shù)組元素當(dāng)作對象來去重。我在微博轉(zhuǎn)發(fā)了用對象去重不是個(gè)好辦法然后作者問什么才是推薦的方法。實(shí)例對象實(shí)例對象主要指通過構(gòu)造函數(shù)類生成的對象。 本文同時(shí)發(fā)布于個(gè)人博客https://www.toobug.net/articl... JavaScript的數(shù)組去重是一個(gè)老生常談的話題了。隨便搜一搜就能找到非常多不同版本的解法。 昨...
摘要:如果數(shù)組已經(jīng)為空,則不改變數(shù)組,并返回值。中所有在數(shù)組被修改時(shí)都遵從這個(gè)原則,以下不再重復(fù)方法會(huì)給原數(shù)組中的每個(gè)元素都按順序調(diào)用一次函數(shù)。每次執(zhí)行后的返回值沒有指定返回值則返回組合起來 數(shù)組應(yīng)該是我們在寫程序中應(yīng)用到最多的數(shù)據(jù)結(jié)構(gòu)了,相比于無序的對象,有序的數(shù)組幫我們在處理數(shù)據(jù)時(shí),實(shí)在是幫了太多的忙了。今天剛好看到一篇Array.include的文章,忽然發(fā)現(xiàn)經(jīng)過幾個(gè)ES3,ES5,E...
摘要:數(shù)組方法大家再熟悉不過了,卻忽略了數(shù)組有這個(gè)方法我個(gè)人感覺。輸出因?yàn)槭菙?shù)組的第個(gè)元素,匹配到并返回下標(biāo)。數(shù)組同樣有方法,只不過做類型判斷時(shí),使用的嚴(yán)格相等,也就是。 本人微信公眾號:前端修煉之路,歡迎關(guān)注 前言 這兩天在家中幫朋友做項(xiàng)目,項(xiàng)目中使用了數(shù)組的indexOf 方法,找到了一篇文章,感覺非常不錯(cuò),順便整理下以防鏈接丟失。 相信說到 indexOf 大家并不陌生,判斷字符串是否...
摘要:支持括號小數(shù)負(fù)數(shù)也行運(yùn)算數(shù)字運(yùn)算符沒有括號正常運(yùn)算括號內(nèi)的值都取完了,刪除括號左右括號齊全先算括號內(nèi)的一個(gè)包含數(shù)字的列表和一個(gè)包含運(yùn)算符的列表形式可能會(huì)有空字符串符號列表同時(shí)出現(xiàn)從左至右運(yùn)算同時(shí)出現(xiàn)從左 public static void main(String[] args) { // 支持括號 小數(shù) 負(fù)數(shù) String statement ...
閱讀 3521·2019-08-30 15:53
閱讀 3435·2019-08-29 16:54
閱讀 2220·2019-08-29 16:41
閱讀 2448·2019-08-23 16:10
閱讀 3402·2019-08-23 15:04
閱讀 1376·2019-08-23 13:58
閱讀 376·2019-08-23 11:40
閱讀 2480·2019-08-23 10:26