Java. One-dimensional array, Java. Dimension Array

Source: Internet
Author: User

Java. One-dimensional array, Java. Dimension Array

One-Bit Array initialization (two methods)

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

Array type array name [] = new data type [constant value] // This method will assign the same default value to all array elements, for the value type, the default value is also 0

 

1. array Traversal

In addition to the for loop, it is relatively simple to use the for-each statement. Format: for (type variable name: array) type: Any data type; array: defined array name

{Statement}

public class bianli {    public static void 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 running

Ps: Method of direct Traversal

Arrays. asList (a) // a is the target array to be traversed.

 

Import java. util. *; public class asList {public static void 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 ());}}

 

 

2: copying Arrays

System. arraycopy (a, B, c, d, e) // a: source array B: Where to start copying c: target array d: Offset e: number of elements to be copied from the source array to the target array

public class arraycopy {   public static void main(String[] args){       String A[]={"H","e","1","1","0"};       String B[]=new String[6];       System.arraycopy(A, 0, B, 1, B.length-1);       for(int i=0;i<B.length;i++){       System.out.print(B[i]+" ");       }       }}

Results After running

3: Fill

Arrays. Fill (a, B) // a: target array name B: value to be filled

import java.util.*;public class tianchong {    public static void main(String[] args) {        // TODO Auto-generated method stub     String[] A=new String[3];     Arrays.fill(A, "I love you");     for(int i=0;i<A.length;i++){         System.out.println(A[i]+" ");     }    }}

After running:

4: equal

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

Import java. util. arrays; public class xiangdeng {public static void 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 ("array A and B are equal:" + Arrays. equals (A, B); System. out. println ("array A and c are equal:" + Arrays. equals (A, C ));}}

After running:

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.