更新時間:2019-09-09 16:10:01 來源:動力節點 瀏覽2438次
今天動力節點java培訓機構小編為大家介紹“Java數組擴容算法及Java對它的應用”,希望此文能夠幫助到大家,下面就隨小編一起看看Java數組擴容算法及Java對它的應用。
Java數組擴容的原理
1、Java數組對象的大小是固定不變的,數組對象是不可擴容的。
2、利用數組復制方法可以變通的實現數組擴容。
3、System.arraycopy()可以復制數組。
4、Arrays.copyOf()可以簡便的創建數組副本。
5、創建數組副本的同時將數組長度增加就變通的實現了數組的擴容。
源碼展示:
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;
}
}
示例說明:
import java.util.Arrays;
/** 數組變長算法!
* 數組對象長度不可改變
* 但是很多實際應用需要長度可變的數組
* 可以采用復制為容量更大的新數組, 替換原數組, 實現變長操作
* */
public class ArrayExpand {
public static void main(String[] args) {
//數組變長(擴容)算法!
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 = { '中', '國' };
chs = Arrays.copyOf(chs, chs.length + 1);
chs[chs.length - 1] = '北';
chs = Arrays.copyOf(chs, chs.length + 1);
chs[chs.length - 1] = '京';
//字符數組按照字符串打印
System.out.println(chs);//中國北京
//其他數組按照對象打印
System.out.println(ary);//[I@4f1d0d
}
}
實現案例:
案例1 : 統計一個字符在字符串中的所有位置.
字符串: 統計一個字符在字符串中的所有位置
字符: '字'
返回: {4,7}
public class CountCharDemo {
public static void main(String[] args) {
char key = '字';
String str = "統計一個字符在字符串中的所有位置";
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){
//擴展數組
count=Arrays.copyOf(count, count.length+1);
//添加序號i
count[count.length-1]=i;
}
}
return count;
}
}
char[]、String、StringBuilder
char[]:字符序列, 只有字符數據, 沒有操作, 如果算法優秀, 性能最好。
String: char[] + 方法(操作, API功能)
StringBuilder: char[] + 方法(操作char[] 的內容)
String:內部包含內容不可變的char[],表現為String對象不可變。String包含操作(API方法),是對char[]操作,但不改變原對象經常返回新的對象,很多String API提供了復雜的性能優化算法,如:靜態字符串池。
StringBuilder:內部也是一個char[],但是這個數組內容是可變的,并且自動維護擴容算法,因為數據內容可變,所以叫:可變字符串。StringBuilder API方法,是動態維護char[]內容,都可以改變char[]內容。
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;
}
/**
* 自動實現Java數組擴容
*/
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);
}
}
字符串數組與String類的原理
/** 字符串數組與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);//中國北京歡迎您
/* 字符串轉大寫: */
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, 原數組ch4不變
String str4="Aacf";
String str5=str4.toUpperCase();//原字符串str4保持不變
System.out.println(str5);//AACF
}
}
以上就是動力節點java培訓機構小編介紹的“Java數組擴容算法及Java對它的應用”的內容,希望能夠幫助到大家,更多java最新資訊請繼續關注動力節點培訓機構官網,每天會有精彩內容分享與你。
相關免費視頻教程推薦
java入門教程下載——數組的擴容:http://m.dabaquan.cn/xiazai/2548.html
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習