实例1.数组的几种定义方式
class arr { public static void main(String[] args) { int[] arr1= new int[2]; int arr2[] = new int[2]; int[] arr3 = new int[]{1,2,3,4,5}; int[] arr4 = {1,2,3,4,5}; System.out.println(arr4[3]); } }
class Arr1 { public static void main(String[] args) { int[] arr = new int[3]; System.out.println("length="+arr.length); //arr.length为数组长度 for(int x=2; x>=0; x--) arr[2-x]=x; for(int x=0; x<3; x++) System.out.println("arr["+x+"]="+arr[x]+";"); int sum=0; for(int x=0; x<3; x++) { sum+=arr[x]; } System.out.println(sum); printArr(arr); } public static void printArr(int[] arr) { for(int x=0; x<3; x++) { System.out.print(arr[x]); if(x!=arr.length-1) System.out.print(", "); } System.out.println(); } }
class Arr2 { public static void main(String[] args) { int[] arr = {22,23,3,4,3,44,32,23,32,23,44,4,4,4,42,1,4,5,6,7,7,8,22,8,9,3}; int max=arr[0], min=arr[0]; for(int i=1; i<arr.length; i++) { if(arr[i]>max) max=arr[i]; if(arr[i]<min) min=arr[i]; } System.out.println("Max="+max+",min="+min); max=Max(arr); min=Min(arr); } public static int Max(int[] arr) { int max=arr[0]; for(int i=1; i<arr.length; i++) { if(arr[i]>max) max=arr[i]; } System.out.println("Max="+max); return max; } public static int Min(int[] arr) { int min=arr[0]; for(int i=1; i<arr.length; i++) { if(arr[i]<min) min=arr[i]; } System.out.println("Min="+min); return min; } }实例4.选择排序与冒泡排序
import java.util.*; //自带排序 class BubbleSort { public static void main(String[] args) { int[] arr = {22,23,3,4,3,44,32,23,32,23,44,4,4,4,42,1,4,5,6,7,7,8,22,8,9,3}; BubbleSort(arr); for(int x=0; x<arr.length; x++) System.out.println("arr["+x+"]="+arr[x]+";"); Arrays.sort(arr); } public static void BubbleSort(int[] arr) { int temp=0; for(int x=0; x<arr.length-1;x++) { for(int y=0;y<arr.length-x-1;y++) { if(arr[y]>arr[y+1]) { /*temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp;*/ swap(arr, y, y+1); } } } //return temp; } public static void SelectSort(int[] arr) { for(int x=0; x<arr.length-1;x++) { for(int y=0;y<arr.length;y++) { if(arr[x]>arr[y]) { swap(arr, x, y); } } } //return temp; } public static void swap(int[] arr, int a, int b) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } }
class Arr4 { public static void main(String[] args) { int[] arr = {22,23,3,4,3,44,32,23,32,23,44,4,4,4,42,1,4,5,6,7,7,8,22,8,9,3}; int i = GetIndex(arr, 4); System.out.println("下标为"+i); int[] arr1={2,3,4,5,6,8,9,10}; int t=HalfSearch(arr1, 5); System.out.println("下标为"+t); t=HalfSearch_2(arr1, 12); System.out.println("下标为"+t); } /* 普通查找 */ public static int GetIndex(int[] arr, int key) { for(int x=0; x<arr.length;x++) { if(key==arr[x]) { return key; } } return -1; } public static int GetIndex_2(int[] arr, int key) { int min=0,max=arr.length-1,mid; while(min<=max) { mid = (max+min)>>1; if(key>arr[mid]) min=mid+1; else if(key<arr[mid]) max=mid-1; else return mid; } return min; } /* 折半_1 */ public static int HalfSearch(int[] arr, int key) { int min, max, mid; min = 0; max = arr.length-1; mid = (max+min)/2; while(arr[mid]!=key) { if(key>arr[mid]) { min=mid+1; mid = (max+min)/2; } else if(key<arr[mid]) { max=mid-1; mid = (max+min)/2; } if(min>max) return -1; } return mid; } /* 折半_2 */ public static int HalfSearch_2(int[] arr, int key) { int min=0,max=arr.length-1,mid; while(min<=max) { mid = (max+min)>>1; if(key>arr[mid]) min=mid+1; else if(key<arr[mid]) max=mid-1; else return mid; } return -1; } }
class Arr5 { public static void main(String[] args) { //System.out.println("Hello World!"); toBin(6); toHex(100); } /* 十进制->二进制 */ public static void toBin(int num) { StringBuffer sb= new StringBuffer(); while(num>0) { //System.out.println(num%2); sb.append(num%2); num = num/2; } System.out.println(sb.reverse()); } /* 十进制->十六 */ public static void toHex(int num) { StringBuffer sb = new StringBuffer(); for(int x=0; x<8; x++) { int temp = num & 15; if(temp>9) //System.out.println((char)temp-10+'A'); sb.append((char)temp-10+'A'); else //System.out.println(temp); sb.append(temp); num = num >>> 4; } System.out.println(sb.reverse()); } }
/* 查表法 */ class Arr6 { public static void main(String[] args) { //System.out.println("Hello World!"); toHex(100); toBin(100); } public static void toBin(int num) { char[] chs ={'0', '1'}; char[] arr = new char[32]; int pos =arr.length; while(num!=0) { int temp = num & 1; arr[--pos] = chs[temp]; num= num >>> 1; } for(int x=pos; x<arr.length; x++) { System.out.print(arr[x]); } } public static void toHex(int num) { char[] arr = new char[8]; int c=arr.length-1; char[] chs = { '0', '1','2','3', '4', '5','6','7', '8', '9','A','B', 'C', 'D','E','F'}; while(num!=0)//for(int x=0; x<8; x++) { int temp = num & 15; //System.out.print(chs[temp]); arr[--c]=chs[temp]; num=num>>>4; } for(int x=c; x<arr.length;x++) { System.out.print(arr[x]); } } }
class JinZhi { public static void main(String[] args) { trans(100,2); //System.out.println("Hello World!"); } public static void trans(int num, int base) { if(num==0) { System.out.println(0); } int offset=0,i =base; while(i!=1) { i/=2; offset++; } char[] arr = new char[32]; int pos=arr.length-1; char[] chs = { '0', '1','2','3', '4', '5','6','7', '8', '9','A','B', 'C', 'D','E','F'}; while(num!=0)//for(int x=0; x<8; x++) { int temp = num & (base-1); //System.out.print(chs[temp]); arr[--pos]=chs[temp]; num=num>>>offset; } for(int x=pos; x<arr.length;x++) { System.out.print(arr[x]); } } }
/* 二维数组 */ class Arr8 { public static void main(String[] args) { //格式1 int[] arr = new int[3][]; //格式2 int[] arr = new int[3][]; arr[0] = new int[3]; arr[1] = new int[1]; arr[2] = new int[2]; //System.out.println("Hello World!"); } }