Today let us brief talk about the array.Array which can be seen in every corner in our programing world and what are its advantages and shortcomings.Hava you really thought about it.
Its adavantages:
1.You can create many objects in one time, in other words you can save time to do other things.
2.You can reach the object you want quikly according to the index.
3.Array is address reference.
4.Array has its center number so it is?easily to check ArrayIndexOutOfBoundsException.
Its shortcomings:
1.Array has its center number. When you not sure how many objects you want to create,this advantage turn into a shortcoming
?
Here is some array Sorting algorithm, when i finished it, i felt i had mastered the array.
class="java" name="code">package 数组排序; import java.util.Arrays; public class Array { int[] a=new int[]{0,5,3,7,46}; public static void main(String[] args){ Array test=new Array(); int[] a=new int[]{0,5,3,7,46}; int MAX=0; //bubble sort for(int i=0;i<5;i++){ for(int j=i;j<5;j++){ if(a[i]<a[j]) { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } } } for(int i=0;i<5;i++){ System.out.print(a[i]+" "); } test.select(); test.arrays(); test.insert(); test.shell(); } //selection sorting public void select(){ System.out.println(); for(int i = 0;i<a.length;i++) { int lowerIndex=i; //find the min index for(int j=i+1;j<a.length;j++){ if(a[j]<a[lowerIndex]) {lowerIndex=j;} } //exchange int temp=a[i]; a[i]=a[lowerIndex]; a[lowerIndex]=temp; } for(int i=0;i<5;i++){ System.out.print(a[i]+" "); } } //which method has saved in JDK public void arrays(){ System.out.println(); Arrays.sort(a); for(int i=0;i<5;i++){ System.out.print(a[i]+" ");} } //insertion sorting public void insert(){ System.out.println(); for(int i=1;i<a.length;i++){ for(int j=i;j>0;j--){ if(a[j]<a[j-1]){ int temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } } } for(int i=0;i<5;i++){ System.out.print(a[i]+" ");} } //shell sorting public void shell(){ System.out.println(); //divide into group for(int increment=a.length/2;increment>0;increment/=2){ //sort in each group for(int i=increment;i<a.length;i++){ int temp=a[i]; int j=0; for(j=i;j>=increment;j-=increment){ if(temp<a[j-increment]) { a[j]=a[j-increment]; }else break; } a[j]=temp; } } for(int i=0;i<5;i++){ System.out.print(a[i]+" ");} } }
?
?