久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

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

因為數(shù)組是在內(nèi)存中連續(xù)的一段存儲空間,所以數(shù)組一旦被創(chuàng)建,空間就固定了,長度是不能擴(kuò)增的。

數(shù)組的長度是固定的,如果需要擴(kuò)充**,必須創(chuàng)建新數(shù)組,原數(shù)組的長度要復(fù)制到新數(shù)組中 。**

java中,數(shù)組類型的變量傳值的時候,事實上傳遞的是數(shù)組的地址 。

Java數(shù)組擴(kuò)容的原理

1)Java數(shù)組對象的大小是固定不變的,數(shù)組對象是不可擴(kuò)容的。

2)利用數(shù)組復(fù)制方法可以變通的實現(xiàn)數(shù)組擴(kuò)容。

3)System.arraycopy()可以復(fù)制數(shù)組。

4)Arrays.copyOf()可以簡便的創(chuàng)建數(shù)組副本。

5)創(chuàng)建數(shù)組副本的同時將數(shù)組長度增加就變通的實現(xiàn)了數(shù)組的擴(kuò)容。

數(shù)組擴(kuò)容的三種方式:

新建一個數(shù)組,把原來數(shù)組的內(nèi)容搬到 新數(shù)組中。

用系統(tǒng)定義函數(shù)system.arraycopy實現(xiàn)擴(kuò)容;

用系統(tǒng)定義函數(shù)copyof函數(shù)實現(xiàn)擴(kuò)容;

下面用程序來實現(xiàn)這三種擴(kuò)容

class expand2{ //利用函數(shù)的方法進(jìn)行數(shù)組的擴(kuò)充 public static void main(String[] args) { //定義一個小型的數(shù)組 int[] a={1,2,3,5}; //調(diào)用擴(kuò)容函數(shù) //a=expand2(a); //a=expand3(a); a=expand4(a); //測試是否擴(kuò)容完成,輸出此時數(shù)組a中的值 for (int i=0;i<a.length;i++) { System.out.println('aaaa:'+a[i]); } } //擴(kuò)容函數(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]; } //返回擴(kuò)容后的數(shù)組b return b; } //數(shù)組擴(kuò)容方法3,利用系統(tǒng)函數(shù)arraycopy進(jìn)行擴(kuò)容 public static int[] expand3(int a[]){ int[] b = new int[a.length*2]; //系統(tǒng)函數(shù)進(jìn)行擴(kuò)容,將a[]的值賦值到b[]中,共a.length個長度。 //相當(dāng)于第19-21行 System.arraycopy(a,0,b,0,a.length); return b; } //數(shù)組擴(kuò)容方法4,利用系統(tǒng)函數(shù)copy進(jìn)行擴(kuò)容 public static int[] expand4(int a[]){ //可以查看api文檔,java.util.Arrays.copyOf的詳細(xì)使用; 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){ //擴(kuò)展數(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提供了復(fù)雜的性能優(yōu)化算法,如:靜態(tài)字符串池。

StringBuilder:內(nèi)部也是一個char[],但是這個數(shù)組內(nèi)容是可變的,并且自動維護(hù)擴(kuò)容算法,因為數(shù)據(jù)內(nèi)容可變,所以叫:可變字符串。StringBuilder API方法,是動態(tài)維護(hù)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ù)組擴(kuò)容 */ 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); }}

字符串?dāng)?shù)組與String類的原理

/** 字符串?dāng)?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[]運(yùn)算需要編程處理,如連接: */ char[] ch3=Arrays.copyOf(ch1, ch1.length+ch2.length); System.arraycopy(ch2, 0, ch3, ch1.length, ch2.length); System.out.println(ch3);//中國北京歡迎您 /* String API提供了簡潔的連接運(yùn)算: */ 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ù)組的三種擴(kuò)容方式以及程序?qū)崿F(xiàn)詳解的文章就介紹到這了,更多相關(guān)java數(shù)組擴(kuò)容內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 宁晋县| 武陟县| 克东县| 新建县| 清水县| 托克逊县| 河西区| 赤峰市| 抚宁县| 安阳县| 平谷区| 福贡县| 镇江市| 东兰县| 阿巴嘎旗| 冕宁县| 定远县| 渝北区| 邯郸县| 潞城市| 湘乡市| 夹江县| 治多县| 南和县| 泽库县| 唐山市| 闻喜县| 金秀| 仙居县| 盘山县| 绥化市| 文水县| 大厂| 正安县| 宾阳县| 上思县| 延寿县| 峨边| 临泉县| 绵竹市| 呼和浩特市|