Learning javase Arrays

Source: Internet
Author: User
Tags array length

Basic concepts of one-dimensional arrays

1. Only one type is allowed in the array (can be a parent-child relationship).

2, the array is the object.
Cases:

    int[ ] arrs={0,1,2};//arrs即一个对象。

3, the array is fixed-length, can not be increased or decreased.

4, declaration array: int[] arrs; The format is: array element type [] array name;
Creating a one-dimensional array must declare the array length: int[] Arrs = new Int[3];

5, initialize the array, if the value is not initialized, the system defaults to 0.
Example of initialization:

    int arr1 = {1,2,3,5,25};(推荐)    int arr2 = new int[] {1,2,3,5,25};

6, the array element Subscript (index) is calculated starting from 0.

Basic Method Array Traversal:
public class Test {    public static void main(String[] args) {        int[] arrs4 = {1,2,3,4,5,6,7,8};        System.out.println("倒序遍历:");        for(int i = arrs4.length-1;i>=0;i--) {            System.out.println(arrs4[i]);        }        System.out.println("foreach遍历:");        for (int arr:arrs4             ) {            System.out.println(arr);//快捷键:sout        }        System.out.println("for遍历:");        for(int i =0;i<arrs4.length;i++) {            System.out.println(arrs4[i]);        }        System.out.println("while遍历");        int k =0;        while(k<arrs4.length) {            System.out.println(arrs4[k]);            k++;        }        System.out.println("do while遍历");        int u =0;        do {            System.out.println(arrs4[u]);            u++;        }while (u<arrs4.length                );          }}
Arrays class

Arrays class: Tool class, Operation array used.
Arrays.fill: array element substitution.
Arrays.copyof: array element copy.
Arrays.binarysearch: array element index.

public class test3 {    public static void main(String[] args) {        int[] arrs = {1,2,3,4};        System.out.println("替换前:"+Arrays.toString(arrs));        Arrays.fill(arrs,2,3,2);        System.out.println("替换后:"+Arrays.toString(arrs));        int[] arrs1 = {1,2,3,4,0,5,4,1,2};        System.out.println("排序前:"+Arrays.toString(arrs1));        Arrays.sort(arrs1);        System.out.println("排序后:"+Arrays.toString(arrs1));        int[] arrs2 = {1,2,3};        int[] arrs21 = new int [4];        System.out.println("复制前"+Arrays.toString(arrs2));        arrs21 = Arrays.copyOf(arrs2,4);        System.out.println("复制后"+Arrays.toString(arrs21));        int[] arrs3 = {1,2,3};        System.out.println(Arrays.binarySearch(arrs3,3));//返回值是元素索引    }}
Basic concepts of two-dimensional arrays

Define a two-dimensional array, at least specify rows, and you can specify no columns.

int[][] arrays = new int[3][];
Basic Method Array Traversal:
public class test2 {    public static void main(String[] args) {        //定义一个数组:        int [][] arrs1= new int[3][];        int[][]arrs2 = {{1,2,3},{2,4},{3,5}};        System.out.println("foreach遍历:");        for (int[] arrs3:arrs2             ) {            for (int arrs4:arrs3                 ) {                System.out.print(arrs4+"\t");            }            System.out.println();        }        System.out.println("for遍历");        for(int i=0;i<arrs2.length;i++) {            for(int j=0;j<arrs2[i].length;j++) {                System.out.print(arrs2[i][j]+"\t");            }            System.out.println();        }        System.out.println("while遍历");        int k = 0;        while(k<arrs2.length) {            int u =0;            while (u<arrs2[k].length) {                System.out.print(arrs2[k][u]+"\t");                u++;            }            System.out.println();            k++;        }    }}

Learning javase Arrays

Related Article

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.