数组的基本功能:
1.数组的组成:
这里使用了泛型,数组可以是任何类型
private int size;//当前元素个数
private int capacity;//容量
private T[]data;//数组保存数据
2.数组的构造方法:
public MyArray(int capacity) {
this.capacity = capacity;
this.size=0;
this.data=(T[])(new Object[this.capacity]);
}
3.获取数组中元素个数
//获取数组元素个数
public int getSize(){
return this.size;
}
4.数组的容积:
//获取数组容积
public int getCapacity(){
return this.capacity;
}
5.判断数组是否为空:
//判断数组是否为空
public boolean isEmity(){
return this.size==0;
}
6.数组容积的变化:
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;
}
7.像数组里添加元素:并扩容:
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++;
}
8.修改指定位置的值:
//修改指定位置值
public void modify(int index,T value){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
this.data[index]=value;
}
9.获取索引位置元素:
//获取指定索引位置元素
public T getValue(int index){
if(index<0||index>this.size){
throw new IllegalArgumentException("index is invalid");
}
return this.data[index];
}
10.查询指定位置的值在数组中是否存在并返回索引
//查询指定位置的值在数组中是否存在,返回索引
public int containsValue(T val){
for(int i=0;i<this.size;i++){
if(val.equals(this.data[i])){
return i;
}
}
return -1;
}
11.删除索引位置数组元素:以及数组的缩容
缩容是/4来判断,/2来缩容文章来源:https://www.toymoban.com/news/detail-817777.html
是为了防止数组的震荡:复杂度的震荡 ,当我们同时进行addLast和removeLast的操作文章来源地址https://www.toymoban.com/news/detail-817777.html
//根据索引删除数组元素
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--;
}
12.tostring方法重写:
@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();
}
13.主函数及部分方法应用:
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);
}
}
}
到了这里,关于数据结构---数组(java)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!