Java自定義長度可變數(shù)組的操作
我們都知道數(shù)組是線性的、類型固定、內(nèi)存地址連續(xù)、定長的,主要是數(shù)組一旦被定義,那么它的長度也就定下來了,只能添加有限的數(shù)據(jù)。而長度可變的數(shù)組是要將這個長度打破,實現(xiàn)數(shù)組數(shù)據(jù)無限增加
那么定義長度可變的數(shù)組就可以用兩個數(shù)組來實現(xiàn)數(shù)組長度的變化。為了避免每次增加數(shù)據(jù)或刪除數(shù)據(jù)時都要重新開辟空間,我先設(shè)定原數(shù)組為固定長,在當(dāng)數(shù)組放滿時,一次增加一定的長度,這樣 節(jié)省了開辟空間的時間
因為數(shù)組里的數(shù)據(jù)類型是不確定的,所以用泛型比較好
public class MyList<E> { private int rongliang;//容量 private int zengliang;//增量 private int num;//數(shù)量 //定義一個原數(shù)組 //Object類包含所有的類型,所以定義數(shù)組是用Object類 private Object[] src; //三個不同的構(gòu)造方法 public MyList(){ this(10,10); } public MyList(int rongliang){ this(rongliang,10); } public MyList(int rongliang,int zengliang){ this.rongliang = rongliang; this.zengliang = zengliang; src = new Object[rongliang]; }}
在MyList中實現(xiàn)在數(shù)組中添加數(shù)據(jù),要考慮到數(shù)組中的數(shù)據(jù)數(shù)量小于數(shù)組長度時,可以直接在數(shù)組為null處添加數(shù)據(jù),但當(dāng)數(shù)組的數(shù)量大于等于數(shù)組長度時,要先重新定義一個數(shù)組,長度是原數(shù)組加增量,然后再添加數(shù)據(jù)
public void add(E s){ //判斷數(shù)組中的數(shù)據(jù)數(shù)量num是否大于數(shù)組的長度(容量),超出則需擴容 if(num>=src.length){ //定義一個新的數(shù)組,長度是原有的長度加增量 Object arr[] = new Object[src.length+zengliang]; //拷貝數(shù)組數(shù)據(jù) System.arraycopy(arr, 0, arr, 0, src.length); src = arr; } //如果num不大于數(shù)組的長度,則不需擴容,直接加入 //如果num大于等于數(shù)組長度,則需執(zhí)行上面的if語句擴容,再加入數(shù)據(jù) //最后num++ src[num++] = s; }
取出指定下標(biāo)的數(shù)據(jù),因為傳入的是下標(biāo)的參數(shù),所以要判斷數(shù)組的下標(biāo)是否越界,拋出異常
public E get(int index){ //拋出異常 if(index<0 || index>=num){ throw new IndexOutOfBoundsException('下標(biāo)越界!index:'+index+',size:'+num); } //強制轉(zhuǎn)換成E類型 return (E)src[index]; }
修改指定下標(biāo)的數(shù)據(jù),因為傳入的是下標(biāo)的參數(shù),所以要判斷數(shù)組的下標(biāo)是否越界,拋出異常
public void modify(int index,E s){ //拋出異常 if(index<0 || index>=num){ throw new IndexOutOfBoundsException('下標(biāo)越界!index:'+index+',size:'+num); } src[index] = s; }
刪除指定下標(biāo)的數(shù)據(jù),當(dāng)數(shù)組中null值的長度大于等于增量時,要將數(shù)組的容量減小,防止浪費
public void delete(int index){ //拋出異常 if(index<0 || index>=num){ throw new IndexOutOfBoundsException('下標(biāo)越界!index:'+index+',size:'+num); } //將>index的數(shù)據(jù)依次向前移動一位 System.arraycopy(src, index+1, src, index, num-index-1); num--; //減少容量的方法 if(src.length-num>=zengliang){ //定義一個新的數(shù)組,長度是原先數(shù)組的長度減去增量 Object arr[] = new Object[src.length-zengliang]; //拷貝數(shù)組 System.arraycopy(src, 0, arr, 0, num); src = arr; } }
將指定下標(biāo)處的數(shù)據(jù)改為指定的數(shù)據(jù)
public void insert(int index,E s){ //拋出異常 if(index<0 || index>=num){ throw new IndexOutOfBoundsException('下標(biāo)越界!index:'+index+',size:'+num); } //判斷數(shù)組中的數(shù)據(jù)數(shù)量num是否大于數(shù)組的長度(容量),超出則需擴容 if(num>=src.length){ //定義一個新的數(shù)組,長度是原有的長度加增量 Object arr[] = new Object[src.length+zengliang]; //拷貝數(shù)組數(shù)據(jù) System.arraycopy(src, 0, arr, 0, src.length); src = arr; } //將>index的數(shù)據(jù)依次向后移動一個位置 //arraycopy()是可以將數(shù)據(jù)自己拷貝給自己 System.arraycopy(src, index, src, index+1, num-index); //插入數(shù)據(jù) src[index] = s; num++; }
最后在寫個獲取數(shù)組中數(shù)據(jù)的個數(shù),而不是數(shù)組的長度
public int size(){ return num; }
寫個測試類,來測試這個長度可變的數(shù)組是否可行
public class test { public static void main(String[] args) { //創(chuàng)建一個MyList對象 // 在創(chuàng)建對象時明確類型 MyList<String> list = new MyList<String>(); //添加數(shù)據(jù) list.add('a'); list.add('b'); list.add('c'); list.add('d'); list.add('e'); list.add('f'); list.add('g'); list.add('h'); list.add('i'); list.add('j'); //遍歷數(shù)組 for(int i=0;i<list.size();i++){ String s = list.get(i); System.out.print(s+' '); } System.out.println(''); int n = list.size(); System.out.println('數(shù)據(jù)個數(shù)為:'+n); System.out.println('**********************************************'); //修改指定位置的數(shù)據(jù) list.modify(1, 'QQ'); //遍歷數(shù)組 for(int i=0;i<list.size();i++){ String s = list.get(i); System.out.print(s+' '); } System.out.println(''); int m = list.size(); System.out.println('數(shù)據(jù)個數(shù)為:'+m); System.out.println('**********************************************'); //刪除指定位置的數(shù)據(jù) list.delete(2); //遍歷數(shù)組 for(int i=0;i<list.size();i++){ String s = list.get(i); System.out.print(s+' '); } System.out.println(''); int k = list.size(); System.out.println('數(shù)據(jù)個數(shù)為:'+k); System.out.println('**********************************************'); //在指定位置插入指定的數(shù)據(jù) list.insert(3, 'zr'); list.insert(3, 'qi'); //遍歷數(shù)組 for(int i=0;i<list.size();i++){ String s = list.get(i); System.out.print(s+' '); } System.out.println(''); int h = list.size(); System.out.println('數(shù)據(jù)個數(shù)為:'+h); System.out.println('**********************************************'); } }
最終數(shù)組的結(jié)果為:
a b c d e f g h i j 數(shù)據(jù)個數(shù)為:10**********************************************a QQ c d e f g h i j 數(shù)據(jù)個數(shù)為:10**********************************************a QQ d e f g h i j 數(shù)據(jù)個數(shù)為:9**********************************************a QQ d qi zr e f g h i j 數(shù)據(jù)個數(shù)為:11**********************************************
補充:在Java中創(chuàng)建一個自定義長度的數(shù)組并輸入每個元素
用到知識點:數(shù)組、方法、Scanner、for循環(huán)。
作業(yè):
package Array;import java.util.Scanner;public class InputArray {public static void main(String[] args) {shuzu();//方法調(diào)用 } //方法定義 public static void shuzu() { //將輸入的數(shù)字作為數(shù)組的長度 Scanner sz = new Scanner(System.in); System.out.println('請輸入數(shù)組長度:');//提示可以操作 int[] cd = new int[sz.nextInt()];//數(shù)組初始化完成 System.out.println('當(dāng)前數(shù)組長度定義為:'+cd.length);//再提示一下結(jié)果 //用for循環(huán)為每一個元素賦值 for (int i = 0; i < cd.length; i++) { int q = i+1;//這里q用作提示,避免提示出第0個元素。 System.out.println('請輸入第'+q+'個元素的值:'); cd [i] = sz.nextInt(); System.out.println('第'+q+'個元素定義為'+cd[i]+'。'); } sz.close(); //數(shù)組內(nèi)各元素已經(jīng)完成賦值,但是再用for循環(huán)遍歷一次 System.out.print('數(shù)組內(nèi)元素全部完成賦值:');//繼續(xù)提示一下 for (int i2 = 0; i2 < cd.length; i2++) { if(i2 == cd.length-1) { System.out.print(cd[i2]+'。'); }else { System.out.print(cd[i2]+'、'); } } return;//方法結(jié)束,rentun; } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. PHP設(shè)計模式中工廠模式深入詳解2. asp中response.write("中文")或者js中文亂碼問題3. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲4. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向5. CSS hack用法案例詳解6. .Net Core和RabbitMQ限制循環(huán)消費的方法7. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明8. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法9. ASP.NET MVC遍歷驗證ModelState的錯誤信息10. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)
