Java数组相关算法一

一、数组反转 

  1、方法一:创建新数组
1 int[] arr = {6,29,0,4,3};
2 int[] arr2 = new int[arr.length];
3 for (int i = 0; i < arr.length; i++) {
4     arr2[i]=arr[arr.length-1-i];
5 }
6 System.out.println(Arrays.toString(arr2));
  2、方法二:交换
1 int[] arr = {6,29,0,4,3};
2 int mid = arr.length/2;
3 for (int i = 0,j=arr.length-1; i < mid; i++,j--) {
4     int a = arr[i];
5     arr[i] = arr[j];
6     arr[j] = a;
7 }
8 System.out.println(Arrays.toString(arr));

二、查找数组中指定元素的索引位置

  1、方法一:遍历
  
 1 public class FindArraryValue {
 2     public static void main(String[] args) {
 3         int[] arr = { 6, 29, 1, 4, 3,100,80,1000,600 };
 4         System.out.println(find1(arr, 6));//1
 5     }
 6 
 7     public static int findValue(int[] a, int v) {
 8         for (int i = 0; i < a.length; i++) {
 9             if (a[i] == v) {
10                 return i;
11             }
12         }
13         return -1;
14     }
15  }
  2、方法二:二分查找法(必须先排序)
 1 public class FindArraryValue {
 2     public static void main(String[] args) {
 3         int[] arr = { 6, 29, 1, 4, 3,100,80,1000,600 };
 4         Arrays.sort(arr);
 5         System.out.println(Arrays.toString(arr));
 6         //[1, 3, 4, 6, 29, 80, 100, 600, 1000]
 7         System.out.println(binaryResearch(arr, 0, arr.length, 6));
 8         //[1, 3, 4, 6, 29, 80, 100, 600, 1000]
 9         System.out.println(Arrays.binarySearch(arr, 6));
10     }
11 
12     public static int binaryResearch(int[] a, int fromIndex, int toIndex, int v) {
13         if (a == null) {
14             throw new RuntimeException("数组不能为空");
15         }
16         if (fromIndex < 0) {
17             throw new RuntimeException("开始索引不能为负数");
18         }
19         if (fromIndex > toIndex) {
20             throw new RuntimeException("开始索引不能小于结束索引");
21         }
22         if (fromIndex > a.length) {
23             throw new RuntimeException("结束索引不能大于数组的长度");
24         }
25         int low = fromIndex;
26         int high = toIndex - 1;
27         while (low <= high) {
28             //无符号右移一位,即除以2
29             int mid = (high + low) >>> 1;
30             if (a[mid] < v) {
31                 low = mid + 1;
32             } else if (a[mid] > v) {
33                 high = mid - 1;
34             } else {
35                 return mid;
36             }
37         }
       //未找到
38 return -1; 39 } 40 }

三、杨辉三角

  1、方法一:直接创建二维数组

 1 public class Yang {
 2     public static void main(String[] args) {
 3         int yang[][] = new int[10][10];
 4 
 5         yang[0][0]=1;
 6         yang[1][0]=1;
 7         yang[1][1]=1;
 8         for (int i = 2; i <10 ; i++) {
 9             yang[i][0]=1;
10             yang[i][i]=1;
11             for (int j = 1; j < i; j++) {
12                 yang[i][j]=yang[i-1][j-1]+yang[i-1][j];
13             }
14         }
15 
16         for (int i = 0; i <10 ; i++) {
17             for (int j = 0; j <= i; j++) {
18                 System.out.print(yang[i][j]+"  ");
19             }
20             System.out.println();
21         }
22     }
23 }    

  2、方法二:在二层循环中创建一位数组(推荐)

 1 public class Yang {
 2     public static void main(String[] args) {
 3         int yang[][] = new int[10][];    
 4         for (int i = 0; i < yang.length; i++) {
 5             yang[i]=new int[i+1];
 6             yang[i][0]=1;
 7             yang[i][i]=1;
 8             //for (int j = 1; j < yang[i].length-1; j++) {
 9             for (int j = 1; j < i; j++) {
10                 yang[i][j]=yang[i-1][j-1]+yang[i-1][j];
11             }
12         }
13         
14         for (int i = 0; i <10 ; i++) {
15             for (int j = 0; j <= i; j++) {
16                 System.out.print(yang[i][j]+"  ");
17             }
18             System.out.println();
19         }
20     }
21 }

四、九九乘法表 

 1 public class Jiujiu {
 2     public static void main(String[] args) {
 3         for (int i = 1; i < 10; i++) {
 4             for (int j = 1; j <= i; j++) {
 5                 System.out.printf("%d * %d = %d   ",j,i,i*j);
 6             }
 7             System.out.println();
 8         }
 9     }
10 }