import java.util.Collection;

public class Main {
    /**
     * This programs works in Java 5.0 o later.
     *
     * The method main must be declared public, static, and void. It
must accept
     * a single argument that is an array of strings. This method can
be
     * declared as either. <br> {...@code public static void main(String
[] args)} <br>
     * or <br> {...@code public static void main(String... args)} <br>
     */
    public static void main(String... args) {

        /*
         * The arrays are objects, are dynamically created, and may be
assigned
         * to variables of type Object. All methods of class Object may be
         * invoked on an array.
         */

        /*
         * An array type is written as the name of an element type followed
by
         * some number of empty pairs of square brackets []. The number of
         * bracket pairs indicates the depth of array nesting. An array’s
length
         * is not part of its type.
         */
        int[] ai; // array of int
        Object[] ao; // array of Object
        Collection<?>[] ca; // array of Collection of unknown type
        short[][] aas; // array of array of short

        /*
         * Here are some examples of declarations of array variables that
create
         * array objects
         */
        Number an[] = new Number[3];
        Number aan[][] = new Integer[2][3];
        int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
        char ac[] = { 'n', 'o', 't', ' ', 'a', ' ', 'S', 't', 'r', 'i', 'n',
                'g' };
        Object[] objects = { new Object(), new Object() };
        double[] doubles = { Math.E, Math.PI };

        // The declaration:
        float[][] matrix1 = new float[3][3];
        // is equivalent in behavior to:
        float[][] matrix2 = new float[3][];
        for (int d = 0; d < matrix2.length; d++) {
            matrix2[d] = new float[3];
        }

        /*
         * A multidimensional array need not have arrays of the same length
at
         * each level. Thus, a triangular matrix may be created by:
         */
        float triang[][] = new float[100][];
        for (int i = 0; i < triang.length; i++) {
            triang[i] = new float[i + 1];
        }

        /* This example prints 3 */
        int[] a = { 11, 12, 13, 14 };
        int[] b = { 0, 1, 2, 3 };
        System.out.println(a[(a = b)[3]]);

        /*
         * This program prints: k==25 and a[0]==25 The value 1 of k is saved
by
         * the compound assignment operator += before its right-hand operand
(k
         * = 4) * (k + 2) is evaluated. Evaluation of this right-hand
         * operand then assigns 4 to
         * k, calculates the value 6 for k + 2, and then multiplies 4 by 6 to
         * get 24. This is added to the saved value 1 to get 25, which is
then
         * stored into k by the += operator. An identical analysis applies to
         * the case that uses a[0]. In short, the statements k += (k = 4) *
(k +
         * 2); a[0] += (a[0] = 4) * (a[0] + 2); behave in exactly the same
         * manner as the statements: k = k + (k = 4) * (k + 2); a[0] = a[0] +
         * (a[0] = 4) * (a[0] + 2);
         */
        int k = 1;
        int[] a2 = { 1 };
        k += (k = 4) * (k + 2);
        a2[0] += (a2[0] = 4) * (a2[0] + 2);
        System.out.println("k==" + k + " and a[0]==" + a2[0]);

        /*
         * Can you do this, but it's not recommendable. I do not recommend
such
         * "mixed notation" for array declarations.
         */
        int c, d[], e[][];
        float[][] f[][], g[][][], h[];

        /*
         * A trailing comma may appear after the last expression in an array
         * initializer and is ignored.
         */
        String[] aas1 = { "array", "of", "String", };
        String[] aas2 = { "array", "of", "String" };

        int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        /*
         * A component of an array is accessed by an array access expression
         * that consists of an expression whose value is an array reference
         * followed by an indexing expression enclosed by [ and ], as in A
[i].
         * All arrays are 0-origin. An array with length n can be indexed by
the
         * integers 0 to n-1. Arrays must be indexed by int values; short,
byte,
         * or char values may also be used as index values because they are
         * subjected to unary numeric promotion and become int values.
         */

        // The example program fills the array with the integers from 0 to
100,
        // sums these integers, and prints the result.
        int[] ia = new int[101];
        for (int i = 0; i < ia.length; i++) {
            ia[i] = i;
        }
        int sum = 0;
        for (int n : ia) {
            sum += n;
        }
        System.out.println(sum); // that produces the output: 5050

        /*
         * Every array implements the interfaces Cloneable and
         * java.io.Serializable. That arrays are cloneable is shown by the
test
         * program:
         */
        int ia1[] = { 1, 2 };
        int ia2[] = ia1.clone();
        System.out.print((ia1 == ia2) + " ");
        ia1[1]++;
        System.out.println(ia2[1]);
        // which prints: false 2
        // showing that the components of the arrays
        // referenced by ia1 and ia2 are different variables.

        /*
         * A clone of a multidimensional array is shallow, which is to say
that
         * it creates only a single new array. Subarrays are shared.
         */
        int ia3[][] = { { 1, 2 }, null };
        int ja[][] = ia3.clone();
        System.out.print((ia3 == ja) + " ");
        System.out.println(ia3[0] == ja[0] && ia3[1] == ja[1]);
        // which prints: false true
        // showing that the int[] array that is ia[0] and the int[] array
that
        // is ja[0] are the same array.

    }

    int sum(int[] a) {
        int sum = 0;
        for (int i : a)
            sum += i;
        return sum;
    }
}

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to