Java学习之数组1(1.数组的声明;2.元素为引用数据类型的数组;3.关于main方法里的String[] args;4.数组排序;5.数3退1 数组算法,(用数组模拟链表);6数组查找之二分法;7数组的拷贝)
1.数组的声明:
Java语言中声明数组时不能指定其长度(数组中元素的个数):
int a[5]; //非法。
int a[][5]; //非法。
2.元素为引用数据类型的数组:
注意:元素为引用数据类型的数组中的每一个元素都需要实例化。
3.关于main方法里的String[] args:
String args[]或者String[] args表示给主方法传一个字符串数组。
1 public class TextNumSort { 2 public static void main(String[] args){ 3 int[] a=new int[args.length]; 4 for(int i=0;i<args.length;i++){ 5 a[i]=Integer.parseInt(args[i]); 6 } 7 Print(a); 8 SelectionSort(a); 9 Print(a); 10 } 11 12 private static void Print(int[] arry){ 13 for(int i=0;i<arry.length;i++){ 14 System.out.print(arry[i]+" "); 15 } 16 System.out.println(); 17 } 18 private static void SelectionSort(int[] arry){ 19 int k,temp; 20 for(int i=0;i<arry.length;i++){ 21 k=i; 22 for(int j=k+1;j<arry.length;j++){ 23 if(arry[j]<arry[k]){ 24 j=k; 25 } 26 if(k!=0){ 27 temp=arry[k]; 28 arry[k]=arry[j]; 29 arry[j]=temp; 30 31 } 32 } 33 } 34 } 35 }
编译时输入数组:41 15 48 54 79 12 41 48 61
输出结果:
12 15 41 41 48 48 54 61 79
4.数组排序:
冒泡法与选择排序法:
1 class Date{ 2 public int year,month,day; 3 Date(int year,int month,int day){ 4 this.year=year; 5 this.month=month; 6 this.day=day; 7 } 8 public int Compare(Date date){ 9 return year>date.year?1:year<date.year?-1:month>date.month?1:month<date.month?-1:day>date.day?1:day<date.day?-1:0; 10 } 11 public String toString(){ 12 return "year,month,day:"+year+"-"+month+"-"+day; 13 } 14 } 15 public class TextBubbleSort { 16 public static void main(String[] args){ 17 Date[] days=new Date[5]; 18 days[0]=new Date(2012,10,1); 19 days[1]=new Date(2011,5,1); 20 days[2]=new Date(2010,4,16); 21 days[3]=new Date(2004,10,16); 22 days[4]=new Date(2007,7,23); 23 Print(days); 24 BubbleSort(days); 25 Print(days); 26 } 27 private static void Print(Date[] a){ 28 for(int i=0;i<a.length;i++){ 29 System.out.print(a[i]+" "); 30 } 31 System.out.println(); 32 } 33 /*public static Date[] SelectionSort(Date[] a){ 34 int len=a.length; 35 int k,i,j; 36 for(i=0;i<len;i++){ 37 for( j=i+1;j<len;j++){ 38 k=i; 39 if(a[j].Compare(a[k])>0){ 40 k=j; 41 } 42 if(k!=0){ 43 Date temp; 44 temp=a[j]; 45 a[j]=a[k]; 46 a[k]=temp; 47 } 48 49 } 50 } 51 return a; 52 }*/ 53 54 public static Date[] BubbleSort(Date[] a){ 55 int len=a.length; 56 for(int i=len-1;i>0;i-- ){ 57 for(int j=0;j<i;j++){ 58 if(a[j].Compare(a[i])>0){ 59 Date temp; 60 temp=a[j]; 61 a[j]=a[i]; 62 a[i]=temp; 63 } 64 } 65 } 66 return a; 67 } 68 69 70 } 71
注意:重写toString方法。
输出结果:
year,month,day:2012-10-1 year,month,day:2011-5-1 year,month,day:2010-4-16 year,month,day:2004-10-16 year,month,day:2007-7-23
year,month,day:2004-10-16 year,month,day:2007-7-23 year,month,day:2010-4-16 year,month,day:2011-5-1 year,month,day:2012-10-1
5. 1.数组算法:
500个人数3退1
1 public class TextCount { 2 public static void main(String[] args){ 3 boolean[] arr=new boolean[500]; 4 for(int i=0;i<arr.length;i++){ 5 arr[i]=true; 6 } 7 int leftCount=arr.length; 8 int countNum=0; 9 int index=0; 10 while(leftCount>1){ 11 if(arr[index]==true){ 12 countNum++; 13 if(countNum==3){ 14 countNum=0; 15 arr[index]=false; 16 leftCount--; 17 } 18 } 19 index++; 20 if(index==arr.length){ 21 index=0; 22 } 23 } 24 for(int i=0;i<arr.length;i++){ 25 if(arr[i]==true){ 26 i=i+1; 27 System.out.println("The last one's number is :"+i); 28 29 } 30 } 31 } 32 }
输出结果:The last one's number is : 436
5.1.2.
500个人数3退1
面向对象的方法:(用数组模拟链表)
1 public class Count3Quit2 { 2 3 public static void main(String[] args) { 4 KidCircle kidCircle = new KidCircle(500); 5 int count = 0; 6 Kid kid = kidCircle.first; 7 while(kidCircle.count>1){ 8 count++; 9 if(count==3){ 10 count=0; 11 kidCircle.delete(kid); 12 } 13 kid = kid.right; 14 } 15 System.out.println(kidCircle.first.id); 16 } 17 } 18 19 20 21 class Kid { 22 int id; 23 Kid right; 24 Kid left; 25 26 Kid(int id) { 27 this.id = id; 28 } 29 } 30 31 class KidCircle{ 32 int count; 33 Kid first; 34 Kid last; 35 36 KidCircle(int n){ 37 for(int i=0;i<n;i++){ 38 add(); 39 } 40 } 41 public void add(){ //默认从尾部添加。 42 Kid kid = new Kid(count); 43 if(count==0){ 44 first = kid; 45 last = kid; 46 kid.right = kid; 47 kid.left = kid; 48 }else{ 49 last.right = kid; 50 kid.left = last; 51 kid.right = first; 52 first.left = kid; 53 last = kid; 54 } 55 count++; 56 } 57 58 public void delete(Kid kid) { 59 if(count<=0)return; 60 else{ 61 kid.left.right = kid.right; 62 kid.right.left = kid.left; 63 if(kid==first) { 64 first = kid.right; 65 }else if(kid==last){ 66 last = kid.left; 67 } 68 count--; 69 } 70 } 71 }
6.数组查找之二分法:
1 public static int binarySearch(int[] array, int value){ 2 int low = 0; //最小元素值的下标 3 int high = array.length - 1; //最大元素值的下标 4 int middle; //中间元素的下标 5 while(low <= high){ 6 middle = (low+high) / 2; 7 for(int i=0; i<array.length; i++){ 8 System.out.print(array[i]); 9 if(i == middle){ 10 System.out.print("#"); //在元素后面用#号标识其为中间元素 11 } 12 System.out.print(" "); //各元素间用空格隔开 13 } 14 System.out.println(); 15 if(value == array[middle]){ 16 return middle; 17 } 18 if(value < array[middle]){ 19 high = middle - 1; //右侧的不要了 20 } 21 if(value > array[middle]){ 22 low = middle + 1; //左侧的不要了 23 } 24 } 25 return -1; //不存在该元素则返回-1 26 } 27 }
7.数组的拷贝:
数组的拷贝:System.arrycopy(args1,args2,args3,args4,args5);
args1:原数组
args2:原数组拷贝的开始位置
args3:目标数组
args4:目标数组放置新元素的开始位置
args5:拷贝的长度
1 public class TextArrayCopy { 2 public static void main(String[] args){ 3 int[] myArray={1,2,3,4,5,6}; 4 int hold[]={10,9,8,7,6,5,4,3,2,}; 5 System.arraycopy(myArray,0,hold,0,myArray.length); 6 for(int i=0;i<hold.length;i++){ 7 System.out.print(hold[i]); 8 } 9 10 11 } 12 }
输出结果:
123456432
二维数组拷贝:
System.arraycopy(strArray[i], 0, copyArray[i], 0, strArray[i].length);