数组
数组特点
1,物理和逻辑上面都是顺序排列。
2,适合读操作多,写操作少情况。
3,插入和删除时间复杂度为O(n);读取和更新时间复杂度为O(1)。
数组基本操作
public class ArrayTest {
private int[] array;
private int size;
public ArrayTest(){
}
public ArrayTest(int capacity){
this.array = new int[capacity];
size = 0;
}
public static void main(String[] args) {
//初始化数组
ArrayTest arrayTest = new ArrayTest(5);
for (int i = 0; i < 5; i++) {
arrayTest.array[i] = i * 3;
arrayTest.size++;
}
System.out.println("原始数组:");
arrayTest.output(arrayTest.array);
}
/**
*
* 数组输出
*/
public void output(int[] array){
for (int i = 0; i < size; i++) {
System.out.print(array[i] + ",");
}
System.out.println();
}
}
1.读取元素
System.out.println(arrayTest.array[3]);
2.更新元素
arrayTest.array[0] = 100;
3.插入元素(包括数组扩容问题)
/**
* @param element 插入的元素
* @param index 插入的位置
* size代表数组的实际长度
*/
public void insert(int element,int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException("超出数组实际元素范围");
}
//若数组的实际长度大于数组初始化长度,则进行数组扩容
if(size >= array.length){
resize();
}
//将插入位置的数据向后移一位
for(int i = size-1;i >= index; i--){
array[i+1] = array[i];
}
array[index] = element;
//数组实际长度加1
size++;
}
/**
* 数组的扩容,默认扩容2倍
*/
public void resize(){
int[] arrayNew = new int[array.length * 2];
//利用for循环进行数组赋值
// for (int i = 0; i < array.length; i++) {
// arrayNew[i] = array[i];
// }
//使用Arrays工具类进行数组赋值
// arrayNew = Arrays.copyOf(array,arrayNew.length);
//利用System进行数组复制
System.arraycopy(array,0,arrayNew,0,array.length);
array = arrayNew;
}
4.删除元素
/**
* 进行元素删除
* @param index 删除位置下标
* @return
*/
public int delete(int index){
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("超出数组实际元素范围!!");
}
int deleteIndex = array[index];
//将元素前移一位
for(int i = index; i < size -1; i++){
array[i] = array[i+1];
}
//将数组的实际长度减一
size--;
return deleteIndex;
}
完整示例代码
import java.util.Arrays;
/**
* 数组中常用的方法
*/
public class ArrayTest {
private int[] array;
private int size;
public ArrayTest(){
}
public ArrayTest(int capacity){
this.array = new int[capacity];
size = 0;
}
public static void main(String[] args) {
ArrayTest arrayTest = new ArrayTest(5);
for (int i = 0; i < 5; i++) {
arrayTest.array[i] = i * 3;
arrayTest.size++;
}
System.out.println("原始数组:");
arrayTest.output(arrayTest.array);
//输出数组中的第四个元素
System.out.println("数组中第四个元素是:" + arrayTest.array[3]);
//将数组中的第一个元素赋值100
arrayTest.array[0] = 100;
//进行元素的插入
System.out.println("插入一个新值:1");
arrayTest.insert(1,0);
arrayTest.insert(11,2);
arrayTest.insert(111,4);
arrayTest.output(arrayTest.array);
//删除数组
arrayTest.delete(2);
arrayTest.output(arrayTest.array);
//输出数组的实际长度
System.out.println("数组实际长度:" + arrayTest.size);
}
/**
* @param element 插入的元素
* @param index 插入的位置
* size代表数组的实际长度
*/
public void insert(int element,int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException("超出数组实际元素范围");
}
//若数组的实际长度大于数组初始化长度,则进行数组扩容
if(size >= array.length){
resize();
}
//将插入位置的数据向后移一位
for(int i = size-1;i >= index; i--){
array[i+1] = array[i];
}
array[index] = element;
//数组实际长度加1
size++;
}
/**
* 数组的扩容,默认扩容2倍
*/
public void resize(){
int[] arrayNew = new int[array.length * 2];
//利用for循环进行数组赋值
// for (int i = 0; i < array.length; i++) {
// arrayNew[i] = array[i];
// }
//使用Arrays工具类进行数组赋值
// arrayNew = Arrays.copyOf(array,arrayNew.length);
//利用
System.arraycopy(array,0,arrayNew,0,array.length);
array = arrayNew;
}
/**
* 进行元素删除
* @param index 删除位置下标
* @return
*/
public int delete(int index){
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("超出数组实际元素范围!!");
}
int deleteIndex = array[index];
//将元素前移一位
for(int i = index; i < size -1; i++){
array[i] = array[i+1];
}
//将数组的实际长度减一
size--;
return deleteIndex;
}
/**
*
* 数组输出
*/
public void output(int[] array){
for (int i = 0; i < size; i++) {
System.out.print(array[i] + ",");
}
System.out.println();
}
}