Core Java (5) Java Array

Source: Internet
Author: User
Array initialization and copying

Package COM. xujin; import Java. util. arrays; public class test {public static void main (string [] ARGs) {int [] A = {2, 3, 5, 7}; int [] B =; // At this time, a and B reference the same Array System. out. println (arrays. tostring (a); // [2, 3, 0, 7] system. out. println (arrays. tostring (B); // [2, 3, 0, 7] B = new int [] {12, 13, 14}; // initialize bsystem with an anonymous array. out. println (arrays. tostring (a); // [2, 3, 0, 7] system. out. println (arrays. tostring (B); // [12, 13, 14] // public static int [] copyof (INT [] original, int newlength) int [] C = arrays. copyof (A,. length); C [2] = 100; system. out. println (arrays. tostring (a); // [2, 3, 0, 7] system. out. println (arrays. tostring (c); // [2, 3,100, 7] // public static void arraycopy (Object SRC, int srcpos, object DEST, int destpos, int length) int [] d = {33, 44, 55, 66, 77, 88, 99}; system. arraycopy (D, 0, A, 1, 3); system. out. println (arrays. tostring (a); // [2, 33, 44, 55] system. out. println (arrays. tostring (d); // [33, 44, 55, 66, 77, 88, 99] // public static int [] copyofrange (INT [] original, int from, int to) int [] e = arrays. copyofrange (A, 1, 3); system. out. println (arrays. tostring (e); // [33, 44]}

Sorting of Arrays

In this test ~ 49 a total of 50 numbers can contain any 6 numbers. The output is sorted by size.

package com.xujin;import java.util.Arrays;public class Test {static final int MAX = 50;static final int FIT = 6;public static void main(String[] args){int[] total = new int[MAX];int[] result = new int[FIT];for(int i = 0; i < MAX; i++)total[i] = i;for(int i = 0; i < FIT; i++){result[i] = (int) (Math.random() * MAX);}Arrays.sort(result);System.out.println(Arrays.toString(result));}}
Test the binary search, filling, and determination of the same Array

Package COM. xujin; import Java. util. arrays; public class test {public static void main (string [] ARGs) {int [] A = new int [100]; for (INT I = 0; I <. length; I ++) A [I] = I; int [] B = arrays. copyofrange (A, 0, 10); system. out. println (arrays. tostring (B); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] // public static int binarysearch (INT [] A, int key) int r = arrays. binarysearch (A, 23); system. out. println (r); // 23 // public static void fill (INT [] A, int Val) arrays. fill (B, 8); system. out. println (arrays. tostring (B); // [8, 8, 8, 8, 8, 8, 8, 8, 8] // public static Boolean equals (INT [] A, int [] A2) int [] C = new int [] {8, 8, 8, 8, 8, 8, 8, 8, 8, 8}; If (arrays. equals (B, c) // returns a truesystem. out. println ("the two arrays are of the same size, and the same elements in the following table are equal ");}}

Two-dimensional array

Package COM. xujin; import Java. util. arrays; public class test {public static void main (string [] ARGs) {int [] [] matrix = {1, 2, 3}, {3, 2, 1}, {4, 5, 6 },}; system. out. println (Matrix [0] [2]); // 3for (INT I = 0; I <matrix. length; I ++) system. out. println (arrays. tostring (Matrix [I]); // [1, 2, 3] // [3, 2, 1] // [4, 5, 6] // quickly print a list of data elements in a two-dimensional array system. out. println (arrays. deeptostring (matrix); // [[1, 2, 3], [3, 2, 1], [4, 5, 6] // For each loop statements cannot automatically process every element of a two-dimensional array. They are for (INT [] row: matrix) for (INT value: row) {system. out. println (value * 10);}/* 102030302010405060 */}}

Irregular Array

Print the following series:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

package com.xujin;public class Test {public static void main(String[] args){int[][] test = new int[10][];for(int n = 0; n < 10; n++){test[n] = new int[n + 1];}for(int i = 0; i < 10; i++)for(int j = 0; j < test[i].length; j++){test[i][j] = j + 1;}for(int[] row : test){for(int value : row)System.out.print(value + " ");System.out.println();}}}

The above program creates an irregular array with 10 elements, respectively test [0] ~ Test [9], each element contains a different number of int types, test [0] contains 1, test [1] contains 2 .... test [9] contains 10

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.