Basic Arrays(基本数组)

public class Arrays {
    public static void main(final String[] args) {
        char [] c; //reference to array(any length of arrays)
        c = new char[4];

        c[0] = 'b';
        c[1] = 'l';
        c[2] = 'u';
        c[3] = 'e';
        for(int i=0; i<4; i++)
        {
            System.out.println(c[i]);
        }

        // the length of array
        int len = c.length;
        System.out.println("the length of array is " + Integer.toString(len));
    }
}

Multi Dimensional Arrays(多维数组)

pascalTriangle(杨辉三角):

public class Multi_dimensional_arrays{
    // two-dimensional array
    public static int[][] pascalTriangle(int n){
        int[][] pt = new int[n][];
        for(int i=0; i<n; i++){
            pt[i] = new int[i+1];
            pt[i][0]=1;
            for(int j=1; j<i; j++){
                pt[i][j] = pt[i-1][j-1]+pt[i-1][j];
            }
            pt[i][i]=1;
        }
        return pt;
    }

    public static void main(String[] args) {
        int n = 10;
        int[][] pt = new int[n][];
        pt = pascalTriangle(n);
        for(int i=0; i<n; i++){
            for(int j=0; j<=i; j++){
                System.out.print(pt[i][j]+" ");
            }
            System.out.print("\n");
        }

    }
}

Resized Arrays(动态数组)

// Resizable arrays
import java.util.*;

public class Resized_array {
    public static void main(String[] args) {
        // Simple fixed-size arrays
        String [] fixedArray = new String[3];
        fixedArray[0] = "This";
        fixedArray[1] = "Cannot";
        fixedArray[2] = "Grow";

        // Resizable arrays
        // create ArrayList of Strings
        List<String> resizable = new ArrayList<String>();
        resizable.add("This");
        resizable.add("Is");
        resizable.add("Resizable");
    }
}
0
Posted in ACM

Leave a Comment:

电子邮件地址不会被公开。