色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

java數(shù)組的三種擴容方式以及程序?qū)崿F(xiàn)詳解

瀏覽:2日期:2022-08-19 09:18:03

因為數(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)!

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产成人午夜精品免费视频 | 在线日韩欧美一区二区三区 | 亚洲国产精品乱码在线观看97 | 在线国产视频 | 91免费视 | 久久久久免费视频 | 亚洲精品成人在线 | 麻豆理论片| 久久午夜国产片 | 中文字幕在线网址 | 作爱在线观看 | 男女朋友做爽爽爽免费视频网 | 99久久精品国产免费 | 国产95在线 | 亚洲 | 国产人成午夜免视频网站 | 狠狠综合久久久久综合 | 欧洲欧美成人免费大片 | 正能量www正能量免费网站 | 热er99久久6国产精品免费 | 国产90后美女露脸在线观看 | 一级毛片免费在线播放 | 自拍偷在线精品自拍偷无码专区 | 九九视频在线观看视频23 | 免费成人高清 | 99精品久久久久久 | 欧美成人做性视频在线播放 | 精品一区二区三区免费站 | 国产成人v视频在线观看 | 亚洲理论视频 | 国产一区自拍视频 | 亚洲性免费 | 国产一级做a爰片久久毛片99 | 综合在线视频精品专区 | 手机在线看片福利 | 在线观看欧美亚洲日本专区 | 亚洲精品91香蕉综合区 | 欧美高清另类自拍视频在线看 | 爽爽免费视频 | 黄网站色成年小说系列 | 欧美一级免费大片 | 久草在线视频中文 |