C,C++的数组都可以分配在栈上,
但是java的数组属于引用类型.
一位数组的声明方式:
type var[]; 或者type[] var;
例如:
int a[]; int[] a2;
Person[] p1; String s1[];
java语言声明数组的时候, 不能指定其长度,例如 int a[5]; //错误的
java的数组是new出来的.
int[] s=new int[5]; //其中s放在栈里, 5个0放在堆里. for(int i=0;i<5;i++){ s[i]=i; }
数组的动态初始化:
public class Test { public static void main(String[] args) { int a[]; a=new int[3]; a[0] = 3; a[1] = 9; a[2] = 8; Date days[]; days=new Date[3]; days[0] = new Date(1,4,2004); days[1] = new Date(2,4,2004); days[2] = new Date(3,4,2004); } } class Date{ int year, month, day; Date(int y, m, d){ year = y; month = m; day = d; } }
数组的静态初始化:
public class Test { public static void main(String[] args) { int a[] = {3, 9, 8}; Date days[] = {
new Date(1,4,2004), new Date(2,4,2004), new Date(3,4,2004) }; } class Date{ int year, month, day; Date(int y, m, d){ year = y; month = m; day = d; } }
数组元素的默认初始化: 对象是null, int 是 0.
数组元素的访问: a[3], c[6*i].
数组的下标范围:0~n-1.
数组的长度: a.length, 注意是属性, 不是方法. 而String的length()是方法.
public class TestArray { public static void main(String[] args){ int a[]={2,4,5,8}; for(int i=0;i<a.length;i++) System.out.println(a[i]+" "); } }
args的详细用法:
public class TestArgs { public static void main(String[] args) { /* for(int i=0; i<args.length; i++) { System.out.println(args[i]); } System.out.println( "Usage: java Test "n1" "op" "n2""); */ if(args.length<3){ System.out.println( "Usage: java Test "n1" "op" "n2""); System.exit(-1); } double d1 = Double.parseDouble(args[0]); double d2 = Double.parseDouble(args[2]); double d = 0; if(args[1].equals("+")) d = d1+d2; else if(args[1].equals("-")) d = d1-d2; else if(args[1].equals("x")) d = d1*d2; else if(args[1].equals("/")) d = d1/d2; else{ System.out.println("Error operator!"); System.exit(-1); } System.out.println(d); } }
基础类型都分配在栈上, 如果想让它存放在堆里, 要把他包装成一个类:
Double.ParseDouble();
选择排序数组的例子:
public class NumSort { public static void main(String[] args) { int[] a = new int[args.length]; for (int i=0; i<args.length; i++) { a[i] = Integer.parseInt(args[i]); } print(a); selectionSort(a); print(a); } private static void selectionSort(int[] a) { int k, temp; for(int i=0; i<a.length; i++) { k = i; for(int j=k+1; j<a.length; j++) { if(a[j] < a[k]) { k = j; } } if(k != i) { temp = a[i]; a[i] = a[k]; a[k] = temp; } } } private static void print(int[] a) { for(int i=0; i<a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } }
冒泡排序Date的算法:
public class TestDateSort { public static void main(String[] args) { Date[] days = new Date[5]; days[0] = new Date(2006, 5, 4); days[1] = new Date(2006, 7, 4); days[2] = new Date(2008, 5, 4); days[3] = new Date(2004, 5, 9); days[4] = new Date(2004, 5, 4); Date d = new Date(2006, 7, 4); String str = String.valueOf(d); //str = d.toString(); bubbleSort(days); for(int i=0; i<days.length; i++) { System.out.println(days[i]); } System.out.println(binarySearch(days, d)); } public static Date[] bubbleSort(Date[] a){ int len = a.length; for(int i = len-1;i>=1;i--){ for(int j = 0;j<=i-1;j++){ if(a[j].compare(a[j+1]) > 0){ Date temp = a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } return a; } public static int binarySearch(Date[] days, Date d) { if (days.length==0) return -1; int startPos = 0; int endPos = days.length-1; int m = (startPos + endPos) / 2; while(startPos <= endPos){ if(d.compare(days[m]) == 0) return m; if(d.compare(days[m]) > 0) { startPos = m + 1; } if(d.compare(days[m]) < 0) { endPos = m -1; } m = (startPos + endPos) / 2; } return -1; } } class Date { int year, month, day; Date(int y, int m, int d) { year = y; month = m; day = d; } public int compare(Date date) { return year > date.year ? 1 : year < date.year ? -1 : month > date.month ? 1 : month < date.month ? -1 : day > date.day ? 1 : day < date.day ? -1 : 0; } public String toString() { return "Year:Month:Day -- " + year + "-" + month + "-" + day; } }
500个人一圈, 数到3退出, 最后剩几号:
public class Count3Quit { public static void main(String[] args) { boolean[] arr = new boolean[500]; for(int i=0; i<arr.length; i++) { arr[i] = true; } int leftCount = arr.length; int countNum = 0; int index = 0; while(leftCount > 1) { if(arr[index] == true) { countNum ++; if(countNum == 3) { countNum = 0; arr[index] = false; leftCount --; } } index ++; if(index == arr.length) { index = 0; } } for(int i=0; i<arr.length; i++) { if(arr[i] == true) { System.out.println(i); } } } }