private int size;//当前元素个数
private int capacity;//容量
private T[]data;//数组保存数据
public MyArray(int capacity) {
this.capacity = capacity;
this.size=0;
this.data=(T[])(new Object[this.capacity]);
}
//获取数组元素个数
public int getSize(){
return this.size;
}
//获取数组容积
public int getCapacity(){
return this.capacity;
}
//判断数组是否为空
public boolean isEmity(){
return this.size==0;
}
private void resize(int newcapacity){
T[]newdata=(T[])(new Object[newcapacity]);
for(int i=0;i<this.size;i++){
newdata[i]=this.data[i];
}
this.capacity=newcapacity;
this.data=newdata;
}
public void add(T item,int index){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
//this.size表示待插入元素的位置
if(this.size==this.capacity){
resize(this.capacity*2);
}
for(int i=size-1;i>=index;i--){
this.data[i+1]=this.data[i];
}
this.data[index]=item;
this.size++;
}
//修改指定位置值
public void modify(int index,T value){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
this.data[index]=value;
}
//获取指定索引位置元素
public T getValue(int index){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
return this.data[index];
}
//查询指定位置的值在数组中是否存在,返回索引
public int containsValue(T val){
for(int i=0;i<this.size;i++){
if(val.equals(this.data[i])){
return i;
}
}
return -1;
}
缩容是/4来判断,/2来缩容
是为了防止数组的震荡:复杂度的震荡 ,当我们同时进行addLast和removeLast的操作
//根据索引删除数组元素
public void removeValue(int index){
if (index < 0 || index > this.size) {
throw new IllegalArgumentException("index is invalid");
}
for (int i = index + 1; i < this.size; i++) {
this.data[i - 1] = this.data[i];
}
if(this.size<this.capacity/4&&this.capacity/2>0){
resize((this.capacity)/2);
}
this.size--;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder("[");
for(int i=0;i<this.size;i++){
sb.append(this.data[i]);
if(i!=this.size-1){
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
public static void main(String[] args) {
MyArray<Integer> myArray=new MyArray<>(3);
Random random=new Random();
myArray.add(10,0);
System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
for(int i=0;i<3;i++){
myArray.add(random.nextInt(20),myArray.size);
}
myArray.add(40,myArray.size);
System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
System.out.println(myArray.toString()+myArray.capacity);
while(!myArray.isEmity()){
myArray.removeValue(0);
System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
}
}
package com.ffyc.learn;
import java.util.Arrays;
import java.util.Random;
public class MyArray<T> {
private int size;//当前元素个数
private int capacity;//容量
private T[]data;//数组保存数据
//构造方法
public MyArray(int capacity) {
this.capacity = capacity;
this.size=0;
this.data=(T[])(new Object[this.capacity]);
}
//获取数组元素个数
public int getSize(){
return this.size;
}
//获取数组容积
public int getCapacity(){
return this.capacity;
}
//判断数组是否为空
public boolean isEmity(){
return this.size==0;
}
private void resize(int newcapacity){
T[]newdata=(T[])(new Object[newcapacity]);
for(int i=0;i<this.size;i++){
newdata[i]=this.data[i];
}
this.capacity=newcapacity;
this.data=newdata;
}
//数组添加元素
public void add(T item,int index){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
//this.size表示待插入元素的位置
if(this.size==this.capacity){
resize(this.capacity*2);
}
for(int i=size-1;i>=index;i--){
this.data[i+1]=this.data[i];
}
this.data[index]=item;
this.size++;
}
//修改指定位置值
public void modify(int index,T value){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
this.data[index]=value;
}
//获取指定索引位置元素
public T getValue(int index){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
return this.data[index];
}
//查询指定位置的值在数组中是否存在,返回索引
public int containsValue(T val){
for(int i=0;i<this.size;i++){
if(val.equals(this.data[i])){
return i;
}
}
return -1;
}
//根据索引删除数组元素
public void removeValue(int index){
if (index < 0 || index > this.size) {
throw new IllegalArgumentException("index is invalid");
}
for (int i = index + 1; i < this.size; i++) {
this.data[i - 1] = this.data[i];
}
if(this.size<this.capacity/4&&this.capacity/2>0){
resize((this.capacity)/2);
}
this.size--;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder("[");
for(int i=0;i<this.size;i++){
sb.append(this.data[i]);
if(i!=this.size-1){
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
public static void main(String[] args) {
MyArray<Integer> myArray=new MyArray<>(3);
Random random=new Random();
myArray.add(10,0);
System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
for(int i=0;i<3;i++){
myArray.add(random.nextInt(20),myArray.size);
}
myArray.add(40,myArray.size);
System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
System.out.println(myArray.toString()+myArray.capacity);
while(!myArray.isEmity()){
myArray.removeValue(0);
System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
}
}
}