今天長(zhǎng)沙牛耳教育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 {
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;
}
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;
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 {
char value[];
int 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;
}
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類的原理
public class CharArrayDemo {
public static void main(String[] args) {
char[] ch1={'中','國(guó)','北','京'};
char[] ch2={'歡','迎','您'};
System.out.println(ch1);//中國(guó)北京
System.out.println(ch2);//歡迎您
char[] ch3=Arrays.copyOf(ch1, ch1.length+ch2.length);
System.arraycopy(ch2, 0, ch3, ch1.length, ch2.length);
System.out.println(ch3);//中國(guó)北京歡迎您
String str1="中國(guó)北京";
String str2="歡迎您";
String str3=str1.concat(str2);
System.out.println(str3);//中國(guó)北京歡迎您
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
}
}
以上就是長(zhǎng)沙牛耳教育java培訓(xùn)機(jī)構(gòu)小編介紹的“Java數(shù)組擴(kuò)容算法及Java對(duì)它的應(yīng)用”的內(nèi)容,希望能夠幫助到大家,更多java最新資訊請(qǐng)繼續(xù)關(guān)注長(zhǎng)沙牛耳教育培訓(xùn)機(jī)構(gòu)官網(wǎng),每天會(huì)有精彩內(nèi)容分享與你。
相關(guān)免費(fèi)視頻教程推薦
java入門教程下載——數(shù)組的擴(kuò)容:http://www.bjpowernode.com/xiazai/2548.html