Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之?dāng)?shù)組
概述
從今天開始, 小白我將帶大家開啟 Jave 數(shù)據(jù)結(jié)構(gòu) & 算法的新篇章.
數(shù)組
數(shù)組 (Array) 是有序數(shù)據(jù)的集合, 在 Java 中 java.util.Arrays
包含用來(lái)操作數(shù)組的各種方法, 比如排序和搜索等. 其所有方法均為靜態(tài)方法, 調(diào)用起來(lái)非常簡(jiǎn)單.
聲明數(shù)組的兩個(gè)方法
方法一:
數(shù)據(jù)類型[] array;
方法二:
數(shù)據(jù)類型 array[];
創(chuàng)建數(shù)組的兩個(gè)方法
方法一:
數(shù)據(jù)類型[] array = new 數(shù)據(jù)類型[n];
int[] array = new int[10];
方法二:
數(shù)據(jù)類型[] arrray = {value1, value2, ...}
int[] array = new int[10];
索引
索引 (Index) 可以幫助我們定位到想要的數(shù)據(jù), 大幅提高數(shù)據(jù)的檢索速度.
自定義數(shù)組
泛型
<E>
示一種指定的數(shù)據(jù)類型, 叫做泛型. E, 取自 Element (元素) 的首字母. 在出現(xiàn) E 的地方, 我們使用一種引用數(shù)據(jù)類型將其替換即可, 表示我們將存儲(chǔ)哪種引用類型的元素.
構(gòu)造函數(shù)
// 有參構(gòu)造 public Array(int capacity){ data = (E[]) new Object[capacity]; size = 0; } // 無(wú)參構(gòu)造 public Array(){ this(10); }
元素操作
// 頭部添加元素 public void addFirst(E element){ // 如果超過數(shù)組最大容量, 扔出異常 if(size == data.length){ throw new RuntimeException("array is full!"); } // 列表所有index及元素后移 for (int i = size - 1; i >= 0; i--) { data[i + 1] = data[i]; } // 數(shù)組第size個(gè)賦值為element data[0] = element; // 數(shù)組大小+1 size++ } // 尾部添加元素 public void addLast(E element){ // 如果超過數(shù)組最大容量, 扔出異常 if(size == data.length){ throw new RuntimeException("array is full!"); } // 數(shù)組第size個(gè)賦值為element data[size] = element; // 數(shù)組大小+1 size++; } // 通過索引添加元素 public void add(int index, E element){ // 如果超過數(shù)組最大容量, 扔出異常 if(size == data.length){ throw new RuntimeException("reached max capacity"); } if(index < 0 || index > size){ throw new RuntimeException("invalid index"); } // 列表所有index及以后的元素后移 for (int i = size-1; i >=index; i--) { data[i + 1] = data[i]; } data[index] = element; size++; }
調(diào)用
public static void main(String[] args) { // 創(chuàng)建數(shù)組 Array array = new Array(10); // 尾部添加 array.addLast(2); array.addLast(3); array.addLast(4); System.out.println(array.toString()); // 頭部添加 array.addFirst(1); array.addFirst(0); System.out.println(array.toString()); // 通過index添加元素 array.add(0, -1); array.add(6, 5); System.out.println(array.toString()); }
輸出結(jié)果:
Array{data=[2, 3, 4, null, null, null, null, null, null, null]}
Array{data=[0, 1, 2, 3, 4, null, null, null, null, null]}
Array{data=[-1, 0, 1, 2, 3, 4, 5, null, null, null]}
完整代碼
import java.util.Arrays; public class Array<E> { private E[] data; // 存放數(shù)據(jù) private int size; // 存放數(shù)組元素個(gè)數(shù) // 有參構(gòu)造 public Array(int capacity){ data = (E[]) new Object[capacity]; size = 0; } // 無(wú)參構(gòu)造 public Array(){ this(10); } // 獲取數(shù)組容量 public int getCapacity(){ return data.length; } // 獲取數(shù)組元素個(gè)數(shù) public int getSize(){ return size; } // 判斷數(shù)組是否為空 public boolean isEmpty(){ return size == 0; } // 頭部添加元素 public void addFirst(E element){ // 如果超過數(shù)組最大容量, 扔出異常 if(size == data.length){ throw new RuntimeException("array is full!"); } // 列表所有index及元素后移 for (int i = size - 1; i >= 0; i--) { data[i + 1] = data[i]; } // 數(shù)組第size個(gè)賦值為element data[0] = element; // 數(shù)組大小+1 size++; } // 尾部添加元素 public void addLast(E element){ // 如果超過數(shù)組最大容量, 扔出異常 if(size == data.length){ throw new RuntimeException("array is full!"); } // 數(shù)組第size個(gè)賦值為element data[size] = element; // 數(shù)組大小+1 size++; } // 通過索引添加元素 public void add(int index, E element){ // 如果超過數(shù)組最大容量, 扔出異常 if(size == data.length){ throw new RuntimeException("reached max capacity"); } if(index < 0 || index > size){ throw new RuntimeException("invalid index"); } // 列表所有index及以后的元素后移 for (int i = size-1; i >=index; i--) { data[i + 1] = data[i]; } data[index] = element; size++; } @Override public String toString() { return "Array{" + "data=" + Arrays.toString(data) + '}'; } public static void main(String[] args) { // 創(chuàng)建數(shù)組 Array array = new Array(10); // 尾部添加 array.addLast(2); array.addLast(3); array.addLast(4); System.out.println(array.toString()); // 頭部添加 array.addFirst(1); array.addFirst(0); System.out.println(array.toString()); // 通過index添加元素 array.add(0, -1); array.add(6, 5); System.out.println(array.toString()); } }
到此這篇關(guān)于Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之?dāng)?shù)組的文章就介紹到這了,更多相關(guān)Java 數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn)
本文主要介紹了java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解二)
這篇文章主要介紹了使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解二)的相關(guān)資料,需要的朋友可以參考下2016-03-03如何通過XML方式配置并實(shí)現(xiàn)Mybatis
這篇文章主要介紹了如何通過XML方式配置并實(shí)現(xiàn)Mybatis,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11JAVA利用HttpClient進(jìn)行HTTPS接口調(diào)用的方法
本篇文章主要介紹了JAVA利用HttpClient進(jìn)行HTTPS接口調(diào)用的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置
這篇文章主要介紹了Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2007-11-11java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)排序算法之樹形選擇排序,結(jié)合具體實(shí)例形式分析了java樹形選擇排序的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05