更新時(shí)間:2019-09-09 16:10:01 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2457次
今天動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)小編為大家介紹“Java數(shù)組擴(kuò)容算法及Java對(duì)它的應(yīng)用”,希望此文能夠幫助到大家,下面就隨小編一起看看Java數(shù)組擴(kuò)容算法及Java對(duì)它的應(yīng)用。
Java數(shù)組擴(kuò)容的原理
1、Java數(shù)組對(duì)象的大小是固定不變的,數(shù)組對(duì)象是不可擴(kuò)容的。
2、利用數(shù)組復(fù)制方法可以變通的實(shí)現(xiàn)數(shù)組擴(kuò)容。
3、System.arraycopy()可以復(fù)制數(shù)組。
4、Arrays.copyOf()可以簡(jiǎn)便的創(chuàng)建數(shù)組副本。
5、創(chuàng)建數(shù)組副本的同時(shí)將數(shù)組長(zhǎng)度增加就變通的實(shí)現(xiàn)了數(shù)組的擴(kuò)容。
源碼展示:
public class Arrays {
/**
* @param original: the array to be copied
* @param newLength: the length of the copy to be returned
* @return a copy of the original array, truncated or padded with zeros
* to obtain the specified length
*/
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
/**
* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
* (This index may lie outside the array.)
* @return a new array containing the specified range from the original array,
* truncated or padded with zeros to obtain the required length
*/
public static int[] copyOfRange(int[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
int[] copy = new int[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
}
示例說(shuō)明:
import java.util.Arrays;
/** 數(shù)組變長(zhǎng)算法!
* 數(shù)組對(duì)象長(zhǎng)度不可改變
* 但是很多實(shí)際應(yīng)用需要長(zhǎng)度可變的數(shù)組
* 可以采用復(fù)制為容量更大的新數(shù)組, 替換原數(shù)組, 實(shí)現(xiàn)變長(zhǎng)操作
* */
public class ArrayExpand {
public static void main(String[] args) {
//數(shù)組變長(zhǎng)(擴(kuò)容)算法!
int[] ary={1,2,3};
ary=Arrays.copyOf(ary, ary.length+1);
ary[ary.length-1]=4;
System.out.println(Arrays.toString(ary));//[1, 2, 3, 4]
//字符串連接原理
char[] chs = { '中', '國(guó)' };
chs = Arrays.copyOf(chs, chs.length + 1);
chs[chs.length - 1] = '北';
chs = Arrays.copyOf(chs, chs.length + 1);
chs[chs.length - 1] = '京';
//字符數(shù)組按照字符串打印
System.out.println(chs);//中國(guó)北京
//其他數(shù)組按照對(duì)象打印
System.out.println(ary);//[I@4f1d0d
}
}
實(shí)現(xiàn)案例:
案例1 : 統(tǒng)計(jì)一個(gè)字符在字符串中的所有位置.
字符串: 統(tǒng)計(jì)一個(gè)字符在字符串中的所有位置
字符: '字'
返回: {4,7}
public class CountCharDemo {
public static void main(String[] args) {
char key = '字';
String str = "統(tǒng)計(jì)一個(gè)字符在字符串中的所有位置";
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);
//添加序號(hào)i
count[count.length-1]=i;
}
}
return count;
}
}
char[]、String、StringBuilder
char[]:字符序列, 只有字符數(shù)據(jù), 沒(méi)有操作, 如果算法優(yōu)秀, 性能最好。
String: char[] + 方法(操作, API功能)
StringBuilder: char[] + 方法(操作char[] 的內(nèi)容)
String:內(nèi)部包含內(nèi)容不可變的char[],表現(xiàn)為String對(duì)象不可變。String包含操作(API方法),是對(duì)char[]操作,但不改變?cè)瓕?duì)象經(jīng)常返回新的對(duì)象,很多String API提供了復(fù)雜的性能優(yōu)化算法,如:靜態(tài)字符串池。
StringBuilder:內(nèi)部也是一個(gè)char[],但是這個(gè)數(shù)組內(nèi)容是可變的,并且自動(dòng)維護(hù)擴(kuò)容算法,因?yàn)閿?shù)據(jù)內(nèi)容可變,所以叫:可變字符串。StringBuilder API方法,是動(dòng)態(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;
}
/**
* 自動(dòng)實(shí)現(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類(lèi)的原理
/** 字符串?dāng)?shù)組與String類(lèi)的原理 */
public class CharArrayDemo {
public static void main(String[] args) {
/* Java 可以將char[]作為字符串處理 */
char[] ch1={'中','國(guó)','北','京'};
char[] ch2={'歡','迎','您'};
System.out.println(ch1);//中國(guó)北京
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);//中國(guó)北京歡迎您
/* String API提供了簡(jiǎn)潔的連接運(yùn)算: */
String str1="中國(guó)北京";
String str2="歡迎您";
String str3=str1.concat(str2);
System.out.println(str3);//中國(guó)北京歡迎您
/* 字符串轉(zhuǎn)大寫(xiě): */
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
}
}
以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)小編介紹的“Java數(shù)組擴(kuò)容算法及Java對(duì)它的應(yīng)用”的內(nèi)容,希望能夠幫助到大家,更多java最新資訊請(qǐng)繼續(xù)關(guān)注動(dòng)力節(jié)點(diǎn)培訓(xùn)機(jī)構(gòu)官網(wǎng),每天會(huì)有精彩內(nèi)容分享與你。
相關(guān)免費(fèi)視頻教程推薦
java入門(mén)教程下載——數(shù)組的擴(kuò)容:http://m.ilovecolors.com.cn/xiazai/2548.html
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743