Java数组扩容的原理
1)Java数组对象的大小是固定不变的,数组对象是不可扩容的。
2)利用数组复制方法可以变通的实现数组扩容。
3)System.arraycopy()可以复制数组。
4)Arrays.copyOf()可以简便的创建数组副本。
5)创建数组副本的同时将数组长度增加就变通的实现了数组的扩容。
源码展示:
1 public class Arrays { 2 /** 3 * @param original: the array to be copied 4 * @param newLength: the length of the copy to be returned 5 * @return a copy of the original array, truncated or padded with zeros 6 * to obtain the specified length 7 */ 8 public static int[] copyOf(int[] original, int newLength) { 9 int[] copy = new int[newLength];10 System.arraycopy(original, 0, copy, 0,11 Math.min(original.length, newLength));12 return copy;13 }14 /**15 * @param original the array from which a range is to be copied16 * @param from the initial index of the range to be copied, inclusive17 * @param to the final index of the range to be copied, exclusive.18 * (This index may lie outside the array.)19 * @return a new array containing the specified range from the original array,20 * truncated or padded with zeros to obtain the required length21 */22 public static int[] copyOfRange(int[] original, int from, int to) {23 int newLength = to - from;24 if (newLength < 0)25 throw new IllegalArgumentException(from + " > " + to);26 int[] copy = new int[newLength];27 System.arraycopy(original, from, copy, 0,28 Math.min(original.length - from, newLength));29 return copy;30 }31 }
示例说明:
1 import java.util.Arrays; 2 3 /** 数组变长算法! 4 * 数组对象长度不可改变 5 * 但是很多实际应用需要长度可变的数组 6 * 可以采用复制为容量更大的新数组, 替换原数组, 实现变长操作 7 * */ 8 public class ArrayExpand { 9 public static void main(String[] args) {10 //数组变长(扩容)算法! 11 int[] ary={1,2,3};12 ary=Arrays.copyOf(ary, ary.length+1);13 ary[ary.length-1]=4;14 System.out.println(Arrays.toString(ary));//[1, 2, 3, 4]15 //字符串连接原理16 char[] chs = { '中', '国' };17 chs = Arrays.copyOf(chs, chs.length + 1);18 chs[chs.length - 1] = '北';19 chs = Arrays.copyOf(chs, chs.length + 1);20 chs[chs.length - 1] = '京';21 //字符数组按照字符串打印22 System.out.println(chs);//中国北京23 //其他数组按照对象打印24 System.out.println(ary);//[I@4f1d0d25 }26 }
实现案例:
案例1 : 统计一个字符在字符串中的所有位置.
字符串: 统计一个字符在字符串中的所有位置 字符: '字' 返回: {4,7}1 public class CountCharDemo { 2 public static void main(String[] args) { 3 char key = '字'; 4 String str = "统计一个字符在字符串中的所有位置"; 5 int[] count=count(str,key); 6 System.out.println(Arrays.toString(count));//[4, 7] 7 } 8 public static int[] count(String str,char key){ 9 int[] count={};10 for(int i=0;i
char[]、String、StringBuilder
char[]:字符序列, 只有字符数据, 没有操作, 如果算法优秀, 性能最好。
String: char[] + 方法(操作, API功能)
StringBuilder: char[] + 方法(操作char[] 的内容)String:内部包含内容不可变的char[],表现为String对象不可变。String包含操作(API方法),是对char[]操作,但不改变原对象经常返回新的对象,很多String API提供了复杂的性能优化算法,如:静态字符串池。
StringBuilder:内部也是一个char[],但是这个数组内容是可变的,并且自动维护扩容算法,因为数据内容可变,所以叫:可变字符串。StringBuilder API方法,是动态维护char[]内容,都可以改变char[]内容。
1 public abstract class AbstractStringBuilder { 2 /** The value is used for character storage.*/ 3 char value[]; 4 /** The count is the number of characters used.*/ 5 int count; 6 /** Returns the length (character count).*/ 7 public int length() { 8 return count; 9 }10 11 public AbstractStringBuilder append(String str) {12 if (str == null)13 str = "null";14 int len = str.length();15 if (len == 0)16 return this;17 int newCount = count + len;18 if (newCount > value.length)19 expandCapacity(newCount);20 str.getChars(0, len, value, count);21 count = newCount;22 return this;23 }24 25 /**26 * 自动实现Java数组扩容27 */28 void expandCapacity(int minimumCapacity) {29 int newCapacity = (value.length + 1) * 2;30 if (newCapacity < 0) {31 newCapacity = Integer.MAX_VALUE;32 } else if (minimumCapacity > newCapacity) {33 newCapacity = minimumCapacity;34 }35 value = Arrays.copyOf(value, newCapacity);36 }37 }
字符串数组与String类的原理
1 /** 字符串数组与String类的原理 */ 2 public class CharArrayDemo { 3 public static void main(String[] args) { 4 /* Java 可以将char[]作为字符串处理 */ 5 char[] ch1={'中','国','北','京'}; 6 char[] ch2={'欢','迎','您'}; 7 System.out.println(ch1);//中国北京 8 System.out.println(ch2);//欢迎您 9 /* char[]运算需要编程处理,如连接: */10 char[] ch3=Arrays.copyOf(ch1, ch1.length+ch2.length);11 System.arraycopy(ch2, 0, ch3, ch1.length, ch2.length);12 System.out.println(ch3);//中国北京欢迎您13 /* String API提供了简洁的连接运算: */14 String str1="中国北京";15 String str2="欢迎您";16 String str3=str1.concat(str2);17 System.out.println(str3);//中国北京欢迎您18 /* 字符串转大写: */19 char[] ch4={'A','a','c','f'};20 char[] ch5=Arrays.copyOf(ch4, ch4.length);21 for(int i=0;i='a' && c<='z'){24 ch5[i]=(char)(c+('A'-'a'));25 }26 }27 System.out.println(ch5);//AACF, 原数组ch4不变28 String str4="Aacf";29 String str5=str4.toUpperCase();//原字符串str4保持不变30 System.out.println(str5);//AACF31 }32 }
原文地址: