Java?數(shù)據(jù)結構與算法系列精講之數(shù)組
概述
從今天開始, 小白我將帶大家開啟 Jave 數(shù)據(jù)結構 & 算法的新篇章.

數(shù)組
數(shù)組 (Array) 是有序數(shù)據(jù)的集合, 在 Java 中 java.util.Arrays包含用來操作數(shù)組的各種方法, 比如排序和搜索等. 其所有方法均為靜態(tài)方法, 調(diào)用起來非常簡單.

聲明數(shù)組的兩個方法
方法一:
數(shù)據(jù)類型[] array;
方法二:
數(shù)據(jù)類型 array[];
創(chuàng)建數(shù)組的兩個方法
方法一:
數(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ù)類型將其替換即可, 表示我們將存儲哪種引用類型的元素.

構造函數(shù)
// 有參構造
public Array(int capacity){
data = (E[]) new Object[capacity];
size = 0;
}
// 無參構造
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個賦值為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個賦值為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());
}
輸出結果:
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ù)組元素個數(shù)
// 有參構造
public Array(int capacity){
data = (E[]) new Object[capacity];
size = 0;
}
// 無參構造
public Array(){
this(10);
}
// 獲取數(shù)組容量
public int getCapacity(){
return data.length;
}
// 獲取數(shù)組元素個數(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個賦值為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個賦值為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());
}
}
到此這篇關于Java?數(shù)據(jù)結構與算法系列精講之數(shù)組的文章就介紹到這了,更多相關Java 數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java用Arrays.asList初始化ArrayList實例方法
在本篇文章里小編給大家分享的是關于Java中使用Arrays.asList初始化ArrayList的知識點內(nèi)容,需要的朋友們參考下。2019-10-10
在IDEA中配置tomcat并創(chuàng)建tomcat項目的圖文教程
這篇文章主要介紹了在IDEA中配置tomcat并創(chuàng)建tomcat項目的圖文教程,需要的朋友可以參考下2020-07-07

