数组简单分为有序数组和无序数组,根据其存储的数据类型,除了基本数据类型外,还有存储对象的数组
这里,我们用java简单实现下有序数组,无序数组和存储对象的数组
无序数组:
public class MyArray { int[] intArr; int arrLen; //存放数组的真实长度 //初始化 public MyArray(int arrLen){ this.arrLen=0; intArr=new int[arrLen]; } //遍历数组元素 public void display(){ for(int i=0;i<arrLen;i++){ System.out.print(intArr[i]); } System.out.println(); } //从头到尾遍历一遍查询对应元素返回下表 public int searchKey(int key){ int index=-1; for(int i=0;i<arrLen;i++){ if(key==intArr[i]){ index=i; break; } } return index; } //在数组最尾追加元素 public void addKey(int key){ intArr[arrLen]=key; arrLen++; } //删除元素 public void removeKey(int key){ int index=-1; //遍历数组查找要删除元素对应下标 for(int i=0;i<arrLen;i++){ if(key==intArr[i]){ index=i; break; } } if(-1!=index){ //将删除数组的下标后面的元素全部前移一位 for(int i=index;i<arrLen-1;i++){ intArr[i]=intArr[i+1]; } arrLen-=1; } } //查找元素返回下标 public int find(int key){ //从头到尾循环比较一遍 for(int i=0;i<this.arrLen;i++){ if(key==this.arr[i]){ return i; } } return -1; } }
无序数组的特点,插入快,因为它就是直接在末尾插入,不用其他操作,而查找,删除,修改就要慢,因为它得从头到尾遍历找到对应元素后才能进行定位操作.
有序数组:
public class MyOrderArray { private int[] arr; private int arrLen; //初始化 public MyOrderArray(int max){ arr=new int[max]; arrLen = 0; } //获取长度方法 public int size(){ return this.arrLen; } //二分查找 public int find(int val){ int lowerIndex=0; int highIndex=this.arrLen-1; int currentIndex; while(true){ currentIndex=(lowerIndex+highIndex)/2; if(val==this.arr[currentIndex]){ return currentIndex; }else if(lowerIndex>=highIndex){ return -1; }else{ if(val>this.arr[currentIndex]){ lowerIndex=currentIndex+1; }else { highIndex=currentIndex-1; } } } } //插入 public void insert(int insertVal){ int j; //循环遍历一遍找到比插入元素大的第一个下标 for(j=0;j<this.arrLen;j++){ if(this.arr[j]>insertVal){ break; } } //查找到的下标后面全部元素往后移一位 for(int i=this.arrLen;i>j;i--){ this.arr[i]=this.arr[i-1]; } this.arr[j]=insertVal; this.arrLen++; } //删除 public boolean delete(int val){ int k=find(val); if(-1==k){ return false; }else{ //删除元素后面的元素向前移一位 for(int i=k;k<this.arrLen;i++){ this.arr[i]=this.arr[i+1]; } this.arrLen--; return true; } } //遍历展示数组 public void display(){ for(int i=0;i<this.arrLen;i++){ System.out.print(this.arr[i]+","); } System.out.println(); } }
有序数组的特点是,查找快,因为使用了二分查找比无序的从头到尾遍历效率要高的多,但是插入就慢了,因为每次插入都要查找下插入的位置,不同于无序的直接在末尾进行插入
存储对象数组:
public class Person { private String lastName; private String firstName; private int age; public Person(String lastName,String firstName,int age){ this.lastName=lastName; this.firstName=firstName; this.age=age; } public void displayPerson(){ System.out.println("lastName="+this.lastName+",firstName="+this.firstName+",age="+this.age); } public String getLastName(){ return this.lastName; } } public class ClassDataArray { private Person[] arr; private int arrLen; public ClassDataArray(int max){ this.arr=new Person[max]; this.arrLen=0; } public Person findPerson(String searchName){ int j; for(j=0;j<this.arrLen;j++){ if(searchName.equals(this.arr[j].getLastName())){ break; } } if(j==this.arrLen){ return null; }else{ return this.arr[j]; } } public void insertPerson(String firstName,String lastName,int age){ this.arr[this.arrLen]=new Person(firstName,lastName,age); this.arrLen++; } public boolean deletePerson(String lastName){ int j; for(j=0;j<this.arrLen;j++){ if(lastName.equals(this.arr[j].getLastName())){ break; } } if(j==this.arrLen){ return false; }else{ for(int i=j;i<this.arrLen;i++){ this.arr[i]=this.arr[i+1]; } this.arrLen--; return true; } } public void displayPerson(){ for(int i=0;i<this.arrLen;i++){ this.arr[i].displayPerson(); } } }
存储对象的数组与无序数组唯一的不同就是存的是对象,所以就不加注释了。
内容来源于网络如有侵权请私信删除
- 还没有人评论,欢迎说说您的想法!