java 数组扩容_Java数组扩容算法及Java对它的应用

Java数组扩容算法及Java对它的应用

Java数组扩容的原理

1)Java数组对象的大小是固定不变的,数组对象是不可扩容的。

2)利用数组复制方法可以变通的实现数组扩容。

3)System.arraycopy()可以复制数组。

4)Arrays.copyOf()可以简便的创建数组副本。

5)创建数组副本的同时将数组长度增加就变通的实现了数组的扩容。

源码展示:

1 public classArrays {2 /**

3 *@paramoriginal: the array to be copied4 *@paramnewLength: the length of the copy to be returned5 *@returna copy of the original array, truncated or padded with zeros6 * to obtain the specified length7 */

8 public static int[] copyOf(int[] original, intnewLength) {9 int[] copy = new int[newLength];10 System.arraycopy(original, 0, copy, 0,11 Math.min(original.length, newLength));12 returncopy;13 }14 /**

15 *@paramoriginal the array from which a range is to be copied16 *@paramfrom the initial index of the range to be copied, inclusive17 *@paramto the final index of the range to be copied, exclusive.18 * (This index may lie outside the array.)19 *@returna 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, intto) {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 returncopy;30 }31 }

示例说明:

1 importjava.util.Arrays;2

3 /**数组变长算法!4 * 数组对象长度不可改变5 * 但是很多实际应用需要长度可变的数组6 * 可以采用复制为容量更大的新数组, 替换原数组, 实现变长操作7 **/

8 public classArrayExpand {9 public static voidmain(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@4f1d0d

25 }26 }

实现案例:

案例1 : 统计一个字符在字符串中的所有位置.

字符串: 统计一个字符在字符串中的所有位置

字符: '字'

返回: {4,7}

1 public classCountCharDemo {2 public static voidmain(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,charkey){9 int[] count={};10 for(int i=0;i

14 count=Arrays.copyOf(count, count.length+1);15 //添加序号i

16 count[count.length-1]=i;17 }18 }19 returncount;20 }21 }

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 classAbstractStringBuilder {2 /**The value is used for character storage.*/

3 charvalue[];4 /**The count is the number of characters used.*/

5 intcount;6 /**Returns the length (character count).*/

7 public intlength() {8 returncount;9 }10

11 publicAbstractStringBuilder 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(intminimumCapacity) {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 classCharArrayDemo {3 public static voidmain(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);//AACF

31 }32 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值