2011年12月23日星期五

第5天第6节: 数组与方法

==definition
int a[] = new int[10] ;
int[] a = new int[10]; 
int a[]={1,3,5,7,9};
both allocates memeoty on heap
a.length  ==>10;

Two steps:
1. int a[] = null;  ==> creates variable a on stack
2. a = new int[10] ==> allocates memory on heap

==reference
int b[]=a;

==2D array

int a[][] = new int[10][2] ;

int[][] a = new int[10][2]; 
int b[][]=a;
a.length ==> 12
a[1].length ==> 2

==Method or Fucntion
naming rule: the first letter of first word is little, the first letter of rest word is capital
function overload: different arg type or different number of args, but not different return type
recursive call: may cause stack overflow

==passing and returning array:
    public static void main (String argvs[] ) {
        int x[]={1,3,5,8};
        int y[];
        y=print(x);
        System.out.println();
        for(int i=0;i<y.length;i++)System.out.print("y["+i+"]="+y[i]+"\t");
        System.out.println();
    }

    public static int[] print(int tx[]) {
        for(int i=0;i<tx.length;i++)System.out.print("tx["+i+"]="+tx[i]+"\t");
        int ty[]={2,4,6};
        return ty;
    }

==Array utility
java.util.Array.sort(y);
System.arraycopy(x,<start_pos>,y,<start_pos>,<num_of_iterm>)

没有评论: