1 package com.array.jun; 2 3 import java.util.Arrays; 4 import java.util.Scanner; 5 6 public class ArrayTest { 7 8 public static int[] arrayname0; //定义数组 9 public static int[] arrayname1 = {0,0,0,0,0}; 10 public static int[] arrayname2 = new int[]{1,2,3,4,5,6}; //静态数组初始化 11 public static int[] arrayname3 = new int[10]; //动态数组初始化 12 13 public static void arrayTest1(){ 14 for (int i : arrayname1) { 15 System.out.print(arrayname1[i] + " "); //foreach遍历第一个静态初始化数组没问题,第二种会抛出数组越界异常 16 } 17 System.out.println(); 18 for (int i = 0; i < arrayname2.length; i++) { 19 System.out.print(arrayname2[i] + " "); //for遍历都没问题 20 } 21 System.out.println("\n" + arrayname2[1]); 22 } 23 24 public static void arrayTest2() { //测试数组静态工具类方法 25 System.out.println(Arrays.binarySearch(arrayname2, 3)); 26 System.out.println(Arrays.binarySearch(arrayname2, 2, 4, 3)); //按值索引 binarySearch(a, fromIndex, toIndex, key) 27 int[] array_b = Arrays.copyOf(arrayname2, 4); 28 // int[] array_b = Arrays.copyOfRange(arrayname2, 2, 5); //数组元素复制 29 System.out.println(Arrays.toString(array_b)); //tostring 30 System.out.println(Arrays.equals(array_b, arrayname1)); //equals比较相等 31 Arrays.fill(arrayname3, 4); //填充 32 System.out.println(Arrays.toString(arrayname3)); 33 Arrays.sort(arrayname2); //排序 34 } 35 36 public static void test(){ //测试方法,方便回调 37 Scanner scan = new Scanner(System.in); 38 switch (scan.nextInt()) { 39 case 0: 40 arrayTest1(); 41 break; 42 case 1: 43 arrayTest2(); 44 break; 45 default: 46 { 47 System.out.println("请重新输入!"); 48 test(); 49 } 50 break; 51 } 52 } 53 54 public static void main(String[] args) { 55 // TODO Auto-generated method stub 56 test(); 57 } 58 59 }
案例:
1 package com.array.jun; 2 3 import java.util.Scanner; 4 /** 5 * 6 * @author pengjun 7 * @version 1.0 8 * 完成一个数字金额转换成人民币读法的工具! 9 */ 10 public class SwitchTools { 11 12 private static String[] hanzi = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; 13 private static String[] unit = {"十","佰","千","万"}; 14 15 /** 16 * @param num 需要被分解的浮点数 17 * @param zheng 整数部分 18 * @param xiao 小数部分 19 * @return 返回一个含整数和小数字符串数组 20 */ 21 public static String[] getNum(double num){ 22 long zheng = (long)num; 23 long xiao = (long)Math.round((num - zheng) * 100); 24 return new String[]{zheng + "",String.valueOf(xiao)}; 25 } 26 27 /** 28 * @param stringNum 被分解的字符串数组 29 * @param numLen0 整数长度 30 * @param numLen1 小数长度 31 * @return result 拼接字符串 32 */ 33 public static String readNum(String[] stringNum){ 34 String result = ""; 35 int numLen0 = stringNum[0].length(); 36 for (int i = 0; i < numLen0; i++) { 37 int num0 = stringNum[0].charAt(i) - 48; 38 if (i != numLen0 - 1 && num0 != 0) { 39 result += hanzi[num0] + unit[numLen0 - i - 2]; 40 } else { 41 result +=hanzi[num0]; 42 } 43 } 44 45 result += "点"; 46 int numLen1 = stringNum[1].length(); 47 for (int j = 0; j < numLen1; j++) { 48 int num1 = stringNum[1].charAt(j) - 48; 49 result +=hanzi[num1]; 50 } 51 52 return result; 53 } 54 55 public static void test(){ 56 Scanner scan = new Scanner(System.in); 57 double num = scan.nextDouble(); 58 System.out.println(readNum(getNum(num))); 59 } 60 61 public static void main(String[] args) { 62 // TODO Auto-generated method stub 63 test(); 64 } 65 66 }