Coding Exercises--java-3-Array __ajax

Source: Internet
Author: User

Welcome to visit the blog Create and output array of arrays-instance-to find the elements of an array

public class Test {public
    static void Main (string[] args) {
        int[] num = {1,2,3,4,5,6,7,8,9,10};
        int sum = 0;
        System.out.println ("The and the elements of the one-dimensional array are:");
        for (int i=0; i<num.length; i++) {
            if (i==9)
                System.out.print (num[i] + "=");
            else
                System.out.print (Num[i] + "+");
            sum = sum + num[i];
        }
        SYSTEM.OUT.PRINTLN (sum);
    }
Array-instance-gets the minimum value of a one-dimensional array
public class Test {public
    static void Main (string[] args) {
        int[] num = {8,3,4,1,6,10};
        SYSTEM.OUT.PRINTLN ("Output one-dimensional array:");
        for (int i=0; i<num.length; i++) {
            System.out.print (Num[i] + "  ");
        }

        int min = num[0];
        for (int j=0; j<num.length-1; J + +) {
            if (min > num[j+1]) {
                min = num[j+1];
            }
        }
        System.out.println ("\ n the minimum value of a one-dimensional array is:" + min);
    }

Two-dimensional array-output
Java creates an array, initializes the element to 0.
public class Test {public
    static void Main (string[] args) {
        int a[][] = new Int[3][4];
        System.out.println ("Output 3 rows of 4 columns of array:");
        for (int i=0; i<a.length; i++) {for
            (int j=0; j<a[i].length; J + +) {
                System.out.print (A[i][j] + "  ");
            }
            System.out.println ();}}

Three-dimensional array-output
public class Test {public
    static void Main (string[] args) {
        int arr[][][] = new int[][][] {{
                1,2,3},{4,5,6}} ,
                {{7,8,9},{10,11,12}}},
                {{13,14,15},{16,17,18}}}
                ;
        for (int i=0; i<arr.length; i++) {
            System.out.println ("the first" + (i+1) + "element of the three-dimensional array is a" +arr[i].length + "dimension array, which reads as follows:"); 
  for (int j=0; j<arr[i].length; J + +) {for
                (int k=0; k<arr[i][j].length; k++) {
                    System.out.print (arr[i][ J][k] + "\ t")
                ;
                System.out.println ();}}}
Instance-Transpose The matrix
public class Test {public
    static void Main (string[] args) {
        int arr[][] = new int[][] {{1,2,3},{4,5,6},{7,8,9}};< C2/>system.out.println ("The matrix before the transpose is:");
        PrintArray (arr);

        int arr2[][] = new Int[arr.length][arr.length];
        for (int i=0; i<arr.length; i++) {for
            (int j=0; j<arr[i].length; j + +)
                arr2[j][i] = arr[i][j];
        }
        System.out.println ("The Matrix after the transpose is:");
        PrintArray (ARR2);
    }

    private static void PrintArray (int[][] arr) {for
        (int i=0; i<arr.length; i++) {for
            (int j=0; j<arr.lengt H J + +) {
                System.out.print (Arr[i][j] + "  ");
            }
            System.out.println ();}}
Example-Finding the traces of a square matrix
public class Test {public
    static void Main (string[] args) {
        int arr[][] = new int[][] {{1,2,3}, {4,5,6}, {7,8,9}} ;
        int tr = 0;
        System.out.println ("Phalanx arr[][] is:");
        for (int i=0; i<arr.length; i++) {for
            (int j=0; j<arr.length; J + +) {
                System.out.print (Arr[i][j] + "  ");
            }
            System.out.println ();
        }

        for (int i=0; i<arr.length; i++)
            tr + = Arr[i][i];
        The trace of System.out.println ("Phalanx arr[][] is:" + tr);
    }
basic operations of arraysBasic operations of arrays-traversing a one-dimensional array
public class Test {public
    static void Main (string[] args) {
        int day[] = new int[] {31, 28, 31, 30, 31, 30, 31, 31 , A, A, n;
        for (int i=0; i<12; i++) {
            System.out.print ((i+1) + "month has" + day[i] + "Day" + "\t\t");
            if ((i+1)%3==0)
                System.out.println ("\ n");}}
Basic operations of arrays-traversing two-dimensional arrays
public class Test {public
    static void Main (string[] args) {
        int b[][] = new int[][] {{1}, {2,3}, {4,5,6}};
        System.out.println ("Two-dimensional array is:");
        for (int k=0; k<b.length; k++) {for
            (int c = 0; c<b[k].length; C + +) {
                System.out.print (B[k][c] + "  " );
            }
            System.out.println ();}}
Basic operations of arrays-traversing two-dimensional arrays (foreach)
public class Test {public
    static void Main (string[] args) {
        int arr2[][] = new int[][] {{3,4,3}, {1,2}};
        for (int x[]:arr2) {for
            (int e:x) {
                System.out.print (E + "  ");
            }
            System.out.println ();}}
Basic operations of arrays-populating array elements (fill)
Import Java.util.Arrays;
public class Test {public
    static void Main (string[] args) {
        int arr[] = new INT[5];
        Arrays.fill (arr, 8);
        for (int i=0; i<arr.length; i++)
            System.out.println ("First" + (i+1) + "element is:" + arr[i]);
    }
Basic operations of arrays-populating array elements (fill)
Import Java.util.Arrays;
public class Test {public
    static void Main (string[] args) {
        int arr[] = new int[] {2, 1};
        Arrays.fill (arr, 1, 3, 8);

        for (int i=0; i<arr.length; i++) {
            System.out.println ("the first" + (i+1) + "element is:" + arr[i]);}}
Basic operations of arrays-sorting
Import Java.util.Arrays;
public class Test {public
    static void Main (string[] args) {
        int arr[] = new int[] {8, 5};
        System.out.println ("The original one-dimensional array is:");
        for (int i=0; i<arr.length; i++) {
            System.out.print (Arr[i] + "  ");
        }
        Arrays.sort (arr);
        System.out.println ("\ n in ascending order array is:");
        for (int i=0; i<arr.length; i++) {
            System.out.print (Arr[i] + "  ");
        }
    }
}
Basic operations of arrays-copying
Import Java.util.Arrays;
public class Test {public
    static void Main (string[] args) {
        int arr[] = new int[] {%, n,};
        System.out.println ("The array before the copy is:");
        for (int i=0; i<arr.length; i++) {
            System.out.print (Arr[i] + "  ");
        }
        int newarr[] = arrays.copyof (arr, 5);
        System.out.println ("\ n copied array is:");
        for (int i=0; i<newarr.length; i++) {
            System.out.print (Newarr[i] + "  ");
        }
    }
}
Basic operations of arrays-copying
Import Java.util.Arrays;
public class Test {public
    static void Main (string[] args) {
        int arr[] = new int[] {
        %,, System.out.println ("The array before the copy is:");
        for (int i=0; i<arr.length; i++) {
            System.out.print (Arr[i] + "  ");
        }
        int newarr[] = arrays.copyofrange (arr, 0, 3);
        System.out.println ("\ n) copy the array subscript 0~3 to the new array:");
        for (int i=0; i<newarr.length; i++) {
            System.out.print (Newarr[i] + "  ");
        }
    }
}
Instance-memory for Billivi, two-dimensional arrays
public class Test {public
    static void Main (string[] args) {
        int num1 = 1024*1024*2;
        int[] arr1 = new INT[NUM1];
        for (int i=0; i<arr1.length; i++)
            arr1[i] = i;
        Gets the total amount of memory consumed and converts the unit to MB
        long memory1 = Runtime.getruntime (). TotalMemory ()/1024/1024;
        System.out.println ("The total amount of memory occupied with one-dimensional arrays is:" + memory1);
        int num2 = 1024*1024;
        int[][] arr2 = new int[num2][2];
        for (int i=0; i<arr2.length; i++) {
            arr2[i][0] = i;
            ARR2[I][1] = i;
        }
        Long Memory2 = Runtime.getruntime (). TotalMemory ()/1024/1024;
        SYSTEM.OUT.PRINTLN (the total amount of memory consumed by the two-dimensional array storage is: "+ Memory2);
    }
Instance-Sort by using the direct insert sort
public class Test {public
    static void Main (string[] args) {
        int[] array = new int[] {20, 40, 90, 30, 80, 70, 50};
        System.out.println ("Sort before:");
        for (int i=0; i<array.length; i++) {
            System.out.print (Array[i] + "  ");
        }
        int tmp;
        Int J;
        for (int i=1; i<array.length; i++) {
            tmp = array[i];
            For (j=i-1 j>=0 && array[j] > tmp; j--) {
                array[j+1] = array[j];
            }
            ARRAY[J+1] = tmp;
        }
        System.out.println ("\ n sort after:");
        for (int i=0; i<array.length; i++) {
            System.out.print (Array[i] + "  ");
        }
    }
}
Instance-bubbling Sort
public class Test {public static void main (string[] args) {int[] array = new int[]
        {63, 4, 24, 1, 3, 13};
        System.out.println ("bubble sort process is:");  for (int i=1; i<array.length; i++) {for (int j=0; j<array.length-i; J + +) {if (array[j)
                    > Array[j+1]) {int tmp = ARRAY[J];
                    ARRAY[J] = array[j+1];
                ARRAY[J+1] = tmp;
            } System.out.print (Array[j] + "");
            } System.out.print ("" ");
            for (int j=array.length-i; j<array.length; J + +) {System.out.print (Array[j] + "");
        } System.out.println ("" "); }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.