目录
一:数组的基础
二:数组二次封装制作可变数组
数组的优点:快速查询(利用索引)ps:数组最好应用与索引有语义的情况
1.定义数组
private T[] data;//保存数据,利用泛型适用于不同的数组类型
private int size;
private int capacity;
2.构造函数
public Myarr(int capacity) {
if (capacity <= 0) {
this.capacity = 10;
} else {
this.capacity = capacity;
this.size = 0;
}
this.data = (T[])(new Object[this.capacity]);
}
3.简单的获取函数
//获取数组中实际存放元素个数
public int getSize() {
return this.size;
}
//获取容积
public int getCapacity() {
return this.capacity;
}
?4.增删改查
//向数组中添加元素
public void add(T item) {
this.data[this.size] = item;
this.size++;
//修改指定位置的值
public void modifyValueByIndex(int index, T value) {
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
this.data[index] = value;
} //根据索引从数组中删除元素
public T removeByIndex(int index) {
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
T devlaue = this.data[index];
for (int i = index + 1; i < this.size; i++) {
this.data[i + 1] = this.data[i];
}
this.size--;
return devlaue;
}
//获取指定索引位置的值
public T getvalueByIndex(int index) {
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
return this.data[index];
}
5.根据索引添加元素
//向数组中指定位置添加元素
public void addInindex(int index,T val){
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
if(this.size==this.capacity){
//扩容
resize(this.capacity*2);
}
//从index位置元素需要进行后移
for(int i=this.size-1;i>=index;i--){
this.data[i+1]=this.data[i];
}
this.data[index]=val;
this.size++;
}
//向数组的头添加元素
public void addIninhead(int index,T val){
addInindex(0,val);
}
5.不断的添加则数组可能溢出所以进行判断并进行扩容
//扩容
private void resize(int newCapacity){
System.out.println("resize:"+newCapacity);
T[] newdata =(T[])(new Object[newCapacity]);
for(int i=0;i<this.size;i++){
newdata[i]=this.data[i];
}
this.data=newdata;
this.capacity=newCapacity;
}
构造方法输出数组
@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();
}
完整具体代码如下
/*
基于java的数组进行二次封装,制作一个可变的数组
*/
//传参一定要判断
public class Myarr <T>{
private T[] data;//保存数据
private int size;
private int capacity;
//构造函数
public Myarr(int capacity) {
if (capacity <= 0) {
this.capacity = 10;
} else {
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 isEmpty() {
return this.size == 0;
}
//向数组中添加元素
public void add(T item) {
this.data[this.size] = item;
this.size++;
}
//修改指定位置的值
public void modifyValueByIndex(int index, T value) {
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
this.data[index] = value;
}
//获取指定索引位置的值
public T getvalueByIndex(int index) {
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
return this.data[index];
}
//查询指定的值在数组中是否存在,存在获取索引,不存在返回-1
public int containsValue(T value) {
for (int i = 0; i < this.size - 1; i++) {
if (this.data[i] == value) {
return i;
}
}
return -1;
}
//根据索引从数组中删除元素
public T removeByIndex(int index) {
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
T devlaue = this.data[index];
for (int i = index + 1; i < this.size; i++) {
this.data[i + 1] = this.data[i];
}
this.size--;
return devlaue;
}
//向数组中指定位置添加元素
public void addInindex(int index,T val){
if (index < 0 || index >= this.size) {
System.out.println("输入位置有误");
}
if(this.size==this.capacity){
//扩容
resize(this.capacity*2);
}
//从index位置元素需要进行后移
for(int i=this.size-1;i>=index;i--){
this.data[i+1]=this.data[i];
}
this.data[index]=val;
this.size++;
}
//向数组的头添加元素
public void addIninhead(int index,T val){
addInindex(0,val);
}
//扩容
private void resize(int newCapacity){
System.out.println("resize:"+newCapacity);
T[] newdata =(T[])(new Object[newCapacity]);
for(int i=0;i<this.size;i++){
newdata[i]=this.data[i];
}
this.data=newdata;
this.capacity=newCapacity;
}
@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();
}