关于动态数组的问题
import java.util.Scanner; import java.util.regex.Pattern; public class TestBubbleSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean flag = true; String input = null; int[] array = new int[10]; int len = 0; while(flag) { input = sc.next(); if(GetSafeNumber.isInteger(input)) { array[len] = Integer.parseInt(input); len ++; if(len == array.length) { int[] newArray = arrayInc(array); array = newArray; } } if(input.equals("quit")) { flag = false; } } System.out.println("Origial:"); ArraySort.dispaly(array, len); System.out.println(); System.out.println("Sorted"); ArraySort.bubbleSort(array, len); ArraySort.dispaly(array, len); } public static int[] arrayInc(int[] array) { int[] inc = new int[10]; int[] newArray = new int[array.length + inc.length]; for(int i = 0; i < array.length; i++) { newArray[i] = array[i]; } return newArray; } /** * 判断是否是整数 * 方法:利用正则表达式 * @param s 需要判断的字符串 * @return 是整数返回真,否则返回假 */ public static boolean isInteger(String s){ Pattern pattern = Pattern.compile("(\\-?)[0-9]*"); if(pattern.matcher(s).matches()) { return true; } return false; } } class ArraySort { public static void bubbleSort(int[] array, int len) { for (int i = 0; i < len; i++) { for(int j = 0; j < len - i - 1; j++) { if(array[j] > array[j + 1]) { int temp; temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void dispaly(int[] array,int len) { for (int i = 0; i < len; i++) { System.out.print(array[i] +" "); } } }
--------------------------------------------------------------------------------第二个优化版本-------------------------------------------------------
/** * @author YoursChang * * 创建时间:2012-8-7 * * 修改时间:2012-8-8 * 内容说明:用arraycopy取代了遍历拷贝 * 部分代码优化 */ import java.util.Scanner; import java.util.regex.Pattern; public class TestBubbleSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean flag = true; String input = null; int[] array = new int[10]; int len = 0; while(flag) { input = sc.next(); if(GetSafeNumber.isInteger(input)) { array[len] = Integer.parseInt(input); len ++; if(len == array.length) { int[] newArray = arrayInc(array); array = newArray; } } if(input.equals("quit")) { flag = false; } } System.out.println("Origial:"); ArraySort.dispaly(array, len); System.out.println(); System.out.println("Sorted"); ArraySort.bubbleSort(array, len); ArraySort.dispaly(array, len); } /** * 动态的增长数组的长度 * @param array 需要增长的数组 * @return 增长了 increase个单位的数组 */ public static int[] arrayInc(int[] array) { int increase = 10; int[] newArray = new int[array.length + increase]; System.arraycopy(array,0,newArray,0,array.length); return newArray; } /** * 判断是否是整数 * 方法:利用正则表达式 * @param s 需要判断的字符串 * @return 是整数返回真,否则返回假 */ public static boolean isInteger(String s){ Pattern pattern = Pattern.compile("(\\-?)[0-9]*"); if(pattern.matcher(s).matches()) { return true; } return false; } } class ArraySort { public static void bubbleSort(int[] array, int len) { for (int i = 0; i < len; i++) { for(int j = 0; j < len - i - 1; j++) { if(array[j] > array[j + 1]) { int temp; temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void dispaly(int[] array,int len) { for (int i = 0; i < len; i++) { System.out.print(array[i] +" "); } } }