1.數組的定義格式
數組的操作
/*
//選擇排序
*/
//冒泡排序
//數組中元素的查找
折半查找
進制轉換
查表法總結
二維數組 (組中的數組)
//數據定義的題目
1.元素類型 [] 數組名=new 元素類型[元素個數或者 數組長度]; int [] arr=new int[5]; //推薦 int arrs[] =new int[5]; 2.靜態初始化方式 不能寫長度 int [] arrs=new int[]{3,1,4,5,6,2}; int [] arrs={3,1,4,5,6,2}; //好像 是在jdk 5.0 后才能使用 3.boolean類型的數組默認值為 false
數組的操作
public static void main(String [] args){ //length 屬性 打印 數組 int [] arr={1,2,4,5,63,8,7,9,5,4}; for(int x=0;x<arr.length;x++){ if(x!=arr.length-1) System.out.println(arr[i]+","); else System.out.println(arr[i]); } int max =getArrMax(arr): System.out.println(max): } /* 獲取最大值 采用 元素值表示 */ public static int getArrMax(int[] arr){ int max=arr[0]; for(int x=0;x<arr.length;x++){ if(arr[x]>max) max=arr[x]; } return max; } /* 采用 數組元素小標 來表示 */ public static int getArrMax(int[] arr){ int max=0; for(int x=0;x<arr.length;x++){ if(arr[x]>arr[max]) max=x; } return arr[max]; }
/*
//選擇排序
*/
public static void main(String[] args){ int [] arr={1,2,5,6,4,7,3}; selSort(arr); System.out.println(Arrays.toString(arr)); } public static void selSort(int[] arr){ for(int x=0;x< arr.length;x++){ for(int y=x;y<arr.length;y++){ if(arr[x]<arr[y]){ int temp=arr[x]; arr[x]=arr[y]; arr[y]=temp; } } } }
//冒泡排序
public static void main(String[] args) { // TODO Auto-generated method stub int [] arr={1,2,5,6,4,7,3}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } //冒泡排序 public static void bubbleSort(int[] arr) { for(int x=0;x<arr.length-1;x++){ //-x:讓每一次比較的元素減少,-1 :不讓下標越界 for (int y = 0; y < arr.length-1-x; y++) { if(arr[y]>arr[y+1]){ int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; } } } } //數組中交換兩個位置的值 public static void swarp(int arr[],int a.int b){ //方法一: int temp=arr[a]; arr[a]=arr[b]; arr[b]=temp; //方法二 arr[a]=arr[a]^arr[b]; arr[b]=arr[a]^arr[b]; arr[a]=arr[a]^arr[b]; }
//數組中元素的查找
//常規 public static void main(String [] args){ int [] arr={3,1,5,4,6,8,9,7,2}; int index=getIndex(arr,2); System.out.println("index="+index): } //獲取key 第一次出現的位置 -1 表示數組中不存在 public static int getIndex(int[] arr,int key){ for(int x=0;x<arr.length;x++){ if(arr[x]==key) return x; } return -1;// 沒有找到 }
折半查找
public static void main(String[] args) { int [] arrs={1,2,3,4,5,6,7,8,9}; System.out.println(halfSearch(arrs,9)); System.out.println(halfSearch_2(arrs,9)); } //折半查找 必須保證數據中的元素是有序的 /* * 折半查找方式1,提高效率,但是必須要保證該數組是有序的數組 */ public static int halfSearch(int[] arr,int key){ int min=0; int max=arr.length-1; int mid=(max+min)/2; while(arr[mid]!=key){ if(key>arr[mid]) min=mid+1; else if(key<arr[mid]) max=mid-1; if(min>max) return -1; mid=(max+min)/2; } return mid; } /* * 折半查找 2 折半查找方式可以用于插入 數據 */ public static int halfSearch_2(int[] arr,int key){ int min=0,max=arr.length,mid; while(min<=max){ mid=(min+max)>>1; if(key>arr[mid]) min=mid+1; else if(key<arr[mid]) max=mid-1; else return mid; } return mid; //如果要在 一個有序數組中加入 一個元素, 返回 mid 就是要插入的位置 //return -1; //表示在查找的時候 沒有找到 }
進制轉換
/** * 十進制 --> 二進制 * @param num */ public static void toBin(int num){ StringBuffer sb=new StringBuffer(); while(num!=0){ sb.append(num%2); num=num/2; } System.out.println(sb.reverse()); } /** * 十進制--->十六進制 * @param num */ public static void toHex(int num){ StringBuffer sb=new StringBuffer(); while(num!=0){ int temp=num & 15; if(temp>9) sb.append((char)(temp-10+'A')); else sb.append(temp); num=num>>>4; } System.out.println(sb.reverse()); } /** * 十進制--->十六進制 * @param num */ public static void toHex_1(int num){ StringBuffer sb=new StringBuffer(); for (int i = 0; i < 8; i++) { int temp=num & 15; if(temp>9) sb.append((char)(temp+55)); // else sb.append(temp); num=num>>>4; } System.out.println(sb.reverse()); } /** * 十進制--->十六進制 * 查表法 可以為負數 * @param num */ public static void toHex_2(int num){ StringBuffer sb=new StringBuffer(); char[] chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //char 的默認值為 '\u0000'; ' ' for (int i = 0; i < 8; i++) { int temp=num & 15; sb.append(chs[temp]); num=num>>>4; } System.out.println(sb.reverse()); } public static void main(String[] args) { toBin(6); toHex_1(60); toHex(-60); toHex_2(-60); }
查表法總結
/** * 各種進制的轉換 * @param num * @param base 與上的基數 * @param offset 偏移量 * @return */ public static String trans(int num,int base,int offset){ char[] chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; if(num==0) return "0"; StringBuffer sb=new StringBuffer(); while(num!=0){ int temp=num & base; sb.append(chs[temp]); num=num>>>offset; } return sb.reverse().toString(); } /** * 十進制 -->二進制 * @param num * @return */ public static String toBinary(int num){ return trans(num,1,1); } /** * 十進制 -->八進制 * @param num * @return */ public static String toEight(int num){ return trans(num,7,3); } /** * 十進制 -->十六進制 * @param num * @return */ public static String toHex(int num){ return trans(num,15,4); } public static void main(String[] args) { System.out.println(toBinary(6)); System.out.println(toBinary(-6)); System.out.println(toEight(60)); System.out.println(toEight(-60)); System.out.println(toHex(60)); System.out.println(toHex(-60)); }
二維數組 (組中的數組)
public static void main(String[] args) { // TODO Auto-generated method stub int [] arr=new int[3]; // 一維數組 //1.初始化 int[][] arr1=new int[3][4]; //定義 了一個二維數組中,二維數組中有3個一維數組, //每個一維數組中有4個元素. System.out.println(arr1[0][1]); //2初始化 int[][] arr2=new int[3][]; arr2[0]=new int[3]; arr2[1]=new int[]{1,2,3}; arr2[2]=new int[]{2}; //3初始化 int[][] arr3={{1,2,3},{0},{2,3}}; int sum=0; for (int i = 0; i < arr3.length; i++) { for (int j = 0; j < arr3[i].length; j++) { sum+=arr3[i][j]; } } System.out.println(sum); }
//數據定義的題目
1.一維數組的定義 int [] x int x [] ; //兩種都可以 2.二維數組的定義 int[][] y, int y[][], int[] y[], 3.注意 int [] x,y[] ; x是一維數組 y 是二位數組 int x[],y[] : x是一維數組 y 是二位數組 a : x[0]=y; //error b: y[0]=x //yes c: y[0][0]=x //error d: x[0][0]=y //error e: y[0][0]=x[0] //yes f: x=y //error
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
