java數(shù)組的三種擴容方式以及程序?qū)崿F(xiàn)詳解
因為數(shù)組是在內(nèi)存中連續(xù)的一段存儲空間,所以數(shù)組一旦被創(chuàng)建,空間就固定了,長度是不能擴增的。
數(shù)組的長度是固定的,如果需要擴充**,必須創(chuàng)建新數(shù)組,原數(shù)組的長度要復制到新數(shù)組中 。**
java中,數(shù)組類型的變量傳值的時候,事實上傳遞的是數(shù)組的地址 。
Java數(shù)組擴容的原理1)Java數(shù)組對象的大小是固定不變的,數(shù)組對象是不可擴容的。
2)利用數(shù)組復制方法可以變通的實現(xiàn)數(shù)組擴容。
3)System.arraycopy()可以復制數(shù)組。
4)Arrays.copyOf()可以簡便的創(chuàng)建數(shù)組副本。
5)創(chuàng)建數(shù)組副本的同時將數(shù)組長度增加就變通的實現(xiàn)了數(shù)組的擴容。
數(shù)組擴容的三種方式:新建一個數(shù)組,把原來數(shù)組的內(nèi)容搬到 新數(shù)組中。
用系統(tǒng)定義函數(shù)system.arraycopy實現(xiàn)擴容;
用系統(tǒng)定義函數(shù)copyof函數(shù)實現(xiàn)擴容;
下面用程序來實現(xiàn)這三種擴容
class expand2{ //利用函數(shù)的方法進行數(shù)組的擴充 public static void main(String[] args) { //定義一個小型的數(shù)組 int[] a={1,2,3,5}; //調(diào)用擴容函數(shù) //a=expand2(a); //a=expand3(a); a=expand4(a); //測試是否擴容完成,輸出此時數(shù)組a中的值 for (int i=0;i<a.length;i++) { System.out.println('aaaa:'+a[i]); } } //擴容函數(shù), public static int[] expand2(int a[]){ //定義一個新數(shù)組b,并為其賦值長度為數(shù)組a的二倍 int b[] = new int[a.length*2]; //將數(shù)組a的元素循環(huán)遍歷到數(shù)組b中 for (int i=0;i<a.length;i++) { b[i] = a[i]; } //返回擴容后的數(shù)組b return b; } //數(shù)組擴容方法3,利用系統(tǒng)函數(shù)arraycopy進行擴容 public static int[] expand3(int a[]){ int[] b = new int[a.length*2]; //系統(tǒng)函數(shù)進行擴容,將a[]的值賦值到b[]中,共a.length個長度。 //相當于第19-21行 System.arraycopy(a,0,b,0,a.length); return b; } //數(shù)組擴容方法4,利用系統(tǒng)函數(shù)copy進行擴容 public static int[] expand4(int a[]){ //可以查看api文檔,java.util.Arrays.copyOf的詳細使用; return java.util.Arrays.copyOf(a,a.length*2); }}實現(xiàn)案例:
案例1 : 統(tǒng)計一個字符在字符串中的所有位置.
字符串: 統(tǒng)計一個字符在字符串中的所有位置
字符: ’字’
返回: {4,7}
public class CountCharDemo { public static void main(String[] args) { char key = ’字’; String str = '統(tǒng)計一個字符在字符串中的所有位置'; int[] count=count(str,key); System.out.println(Arrays.toString(count));//[4, 7] } public static int[] count(String str,char key){ int[] count={}; for(int i=0;i<str.length();i++){ char c=str.charAt(i); if(c==key){ //擴展數(shù)組 count=Arrays.copyOf(count, count.length+1); //添加序號i count[count.length-1]=i; } } return count; }}
char[]、String、StringBuilder
char[]:字符序列, 只有字符數(shù)據(jù), 沒有操作, 如果算法優(yōu)秀, 性能最好。
String: char[] + 方法(操作, API功能)
StringBuilder: char[] + 方法(操作char[] 的內(nèi)容)
String:內(nèi)部包含內(nèi)容不可變的char[],表現(xiàn)為String對象不可變。String包含操作(API方法),是對char[]操作,但不改變原對象經(jīng)常返回新的對象,很多String API提供了復雜的性能優(yōu)化算法,如:靜態(tài)字符串池。
StringBuilder:內(nèi)部也是一個char[],但是這個數(shù)組內(nèi)容是可變的,并且自動維護擴容算法,因為數(shù)據(jù)內(nèi)容可變,所以叫:可變字符串。StringBuilder API方法,是動態(tài)維護char[]內(nèi)容,都可以改變char[]內(nèi)容。
public abstract class AbstractStringBuilder { /** The value is used for character storage.*/ char value[]; /** The count is the number of characters used.*/ int count; /** Returns the length (character count).*/ public int length() { return count; } public AbstractStringBuilder append(String str) { if (str == null) str = 'null'; int len = str.length(); if (len == 0) return this; int newCount = count + len; if (newCount > value.length) expandCapacity(newCount); str.getChars(0, len, value, count); count = newCount; return this; } /** * 自動實現(xiàn)Java數(shù)組擴容 */ void expandCapacity(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; } else if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } value = Arrays.copyOf(value, newCapacity); }}
字符串數(shù)組與String類的原理
/** 字符串數(shù)組與String類的原理 */public class CharArrayDemo { public static void main(String[] args) { /* Java 可以將char[]作為字符串處理 */ char[] ch1={’中’,’國’,’北’,’京’}; char[] ch2={’歡’,’迎’,’您’}; System.out.println(ch1);//中國北京 System.out.println(ch2);//歡迎您 /* char[]運算需要編程處理,如連接: */ char[] ch3=Arrays.copyOf(ch1, ch1.length+ch2.length); System.arraycopy(ch2, 0, ch3, ch1.length, ch2.length); System.out.println(ch3);//中國北京歡迎您 /* String API提供了簡潔的連接運算: */ String str1='中國北京'; String str2='歡迎您'; String str3=str1.concat(str2); System.out.println(str3);//中國北京歡迎您 /* 字符串轉(zhuǎn)大寫: */ char[] ch4={’A’,’a’,’c’,’f’}; char[] ch5=Arrays.copyOf(ch4, ch4.length); for(int i=0;i<ch5.length;i++){ char c=ch5[i]; if(c>=’a’ && c<=’z’){ ch5[i]=(char)(c+(’A’-’a’)); } } System.out.println(ch5);//AACF, 原數(shù)組ch4不變 String str4='Aacf'; String str5=str4.toUpperCase();//原字符串str4保持不變 System.out.println(str5);//AACF }}
到此這篇關(guān)于java數(shù)組的三種擴容方式以及程序?qū)崿F(xiàn)詳解的文章就介紹到這了,更多相關(guān)java數(shù)組擴容內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 不要在HTML中濫用div2. Vue3使用JSX的方法實例(筆記自用)3. 使用css實現(xiàn)全兼容tooltip提示框4. CSS代碼檢查工具stylelint的使用方法詳解5. vue實現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細教程6. 詳解CSS偽元素的妙用單標簽之美7. html清除浮動的6種方法示例8. CSS3實例分享之多重背景的實現(xiàn)(Multiple backgrounds)9. JavaScript數(shù)據(jù)類型對函數(shù)式編程的影響示例解析10. 利用CSS3新特性創(chuàng)建透明邊框三角
