Java. One-dimensional arrays

Source: Internet
Author: User

Initialization of an array (two methods)

Array type array []={value 1, value 2, value 3 ... Value n};

Array type array name []=new data type [constant value]//This method assigns the same default value to all array elements, and the default value is 0 for numeric types

1. Iterating the array

In addition to the For loop, the simpler is the For-each statement; format: for (type variable name: array) Type: any data type; array: defined array name

                                                                                      {statement}

 Public class Bianli {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub           int a[]={1,2,3,4,5,6,7};     for int i:a) {       System.out.print (i);}}    }

Results after the run

Ps: Method of direct traversal

Arrays.aslist (a)//a is the target array to traverse

import java.util.*;  Public class aslist {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub  string[] a={"I", "L", "O", "V", "E", "J", "a", "V", "a"};  System.out.println ("Array A:" +Arrays.aslist (a));}    }

2: Copying of arrays

System.arraycopy (a,b,c,d,e)//a: source array B: from where to start copying C: Destination array D: Offset E: The number of elements to copy from the source array to the destination array

 Public class arraycopy {   publicstaticvoid  main (string[] args) {       String a[]= {"H", "E", "1", "1", "0"};       String b[]=new string[6];        0, B, 1, b.length-1);         for (int i=0;i<b.length;i++) {       System.out.print (B[i]+ "");        }}}

Results after the run

3: Fill

Arrays.fill (b)//A: Target array name B: The value to be populated

import java.util.*;  Public class Tianchong {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub     string[] a=new string[3];     " I Love You ");       for (int i=0;i<a.length;i++) {         System.out.println (A[i]+ "");     }}}

After running:

4: Equal

Arrays.equals (b)//target array A and target array B

Import java.util.Arrays;  Public class Xiangdeng {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub        string[] a={"A", "B", "C"};        String[] B={"A", "B", "C"};        String[] C={"A", "B", "C"};        System.out.println ("Arrays A and B are equal:" +arrays.equals (A, b));        System.out.println ("Arrays A and C are equal:" +arrays.equals (A, c));}    }

After running:

Java. One-dimensional array

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.