更新時間:2020-11-05 17:41:16 來源:動力節(jié)點 瀏覽1271次
當數(shù)組定義完成后,數(shù)組存儲元素的個數(shù)就確定了,因為在定義數(shù)組時,要指定數(shù)組的長度. 如果想要在數(shù)組中存儲更多的數(shù)據(jù), 就需要對數(shù)組擴容。本文就來為大家介紹Java數(shù)組擴容的相關知識。
想要掌握Java數(shù)組擴容,必須要先了解數(shù)組擴容的一些特性:
1)Java數(shù)組對象的大小是固定不變的,數(shù)組對象是不可擴容的。
2)利用數(shù)組復制方法可以變通的實現(xiàn)數(shù)組擴容。
3)System.arraycopy()可以復制數(shù)組。
4)Arrays.copyOf()可以簡便的創(chuàng)建數(shù)組副本。
5)創(chuàng)建數(shù)組副本的同時將數(shù)組長度增加就變通的實現(xiàn)了數(shù)組的擴容。
package com.wkcto.chapter03.demo01;
import java.util.Arrays;
/**
* 數(shù)組擴容
* @author 蛙課網(wǎng)
*
*/
public class Test06 {
public static void main(String[] args) {
// m1(); //完全手動擴容
// m2(); //數(shù)組復制調(diào)用 了System.arraycopy(0方法
m3(); //調(diào)用 Arrays.copyOf(0實現(xiàn)擴容
}
private static void m3() {
// 定義長度為5的數(shù)組
int[] data = { 1, 2, 3, 4, 5 };
// 想要在數(shù)組中存儲更多的數(shù)據(jù),需要對數(shù)組擴容
//Arrays工具類copyOf(源數(shù)組, 新數(shù)組的長度) 可以實現(xiàn)數(shù)組的擴容
data = Arrays.copyOf(data, data.length*3/2);
System.out.println( Arrays.toString(data));
}
private static void m2() {
//定義長度為5的數(shù)組
int [] data = {1,2,3,4,5};
//想要在數(shù)組中存儲更多的數(shù)據(jù),需要對數(shù)組擴容
//(1) 定義一個更大的數(shù)組
int [] newData = new int[data.length * 3 / 2] ; //按1.5倍大小擴容
//(2)把原來數(shù)組的內(nèi)容復制到新數(shù)組中
//把src數(shù)組從srcPos開始的length個元素復制到dest數(shù)組的destPos開始的位置
// System.arraycopy(src, srcPos, dest, destPos, length);
System.arraycopy(data, 0, newData, 0, data.length);
//arraycopy()方法使用了native修飾,沒有方法體, 該方法的方法體可能是由C/C++實現(xiàn)的
//JNI,Java native Interface技術(shù),可以在Java語言中調(diào)用其他語言編寫的代碼
//(3) 讓原來的數(shù)組名指向新的數(shù)組
data = newData;
//
System.out.println( Arrays.toString(data));
}
private static void m1() {
//1)定義長度為5的數(shù)組
int [] data = {1,2,3,4,5};
//2)想要在數(shù)組中存儲更多的數(shù)據(jù),需要對數(shù)組擴容
//(1) 定義一個更大的數(shù)組
int [] newData = new int[data.length * 3 / 2] ; //按1.5倍大小擴容
//(2)把原來數(shù)組的內(nèi)容復制到新數(shù)組中
for( int i = 0 ; i < data.length; i++){
newData[i] = data[i];
}
//(3) 讓原來的數(shù)組名指向新的數(shù)組
data = newData;
//
System.out.println( Arrays.toString(data));
}
}
在編寫一些非參學習算法時,例如DP和HDP,經(jīng)常會遇到生成新簇的情形,這種情況下,數(shù)組的空間就不夠用了,需要對原來的數(shù)組進行擴容。
例如:
int K=10;
int[] tables = new int[K]; //可以看出該數(shù)組最多可存儲10個元素
for (int i = 0; i<k; p="" {<="">
tables [i] = i; //數(shù)組賦值
}
如何讓上面已經(jīng)賦值的數(shù)組擴展到可存儲11個元素、12個元素等等呢?
針對二維數(shù)組,如下:
int C =10;
int[][] tablesNum = new int[C][10];
for (int i = 0; i < tablesNum.length; i++) {
for (int j = 0; j < tablesNum[i].length; j++) {
tablesNum[i][j] = i*j; //二維數(shù)組賦值
}
}
可以看出該二維數(shù)組最多存儲100個元素,如何讓其存儲更多的元素呢?
解決程序
如下我提供了針對一維數(shù)組和二維數(shù)組擴容的方法,主要使用的是:System.arraycopy()方法。
//將數(shù)組放大,確保不越界
public static int[] ensureCapacity(int[] arr,int i) {
int length = arr.length;
int[] arr2 = new int[length+i];
System.arraycopy(arr, 0, arr2, 0, length);
return arr2;
}
//將數(shù)組放大,確保不越界
public static int[][] ensureCapacity(int[][] array,int i,int j) {
int[][] arr = new int[array.length +i][array[0].length +j];
//擴展
for(int c = 0; c< array.length; c++) {
//數(shù)組拷貝
System.arraycopy(array[c], 0, arr[c], 0, array[c].length);
}
return arr;
}
以上就是Java數(shù)組擴容的相關知識,通過本文中的實例講解,大家對Java數(shù)組擴容的疑問應該一掃而空了。想要學習更多的Java數(shù)組相關知識可以觀看本站的Java基礎教程,全方位為你打好Java基礎。