Home > 类库下载 > java类库 > Java.One-dimensional array

Java.One-dimensional array

高洛峰
Release: 2016-10-17 09:24:31
Original
2007 people have browsed it

Initialization of one-bit array (two methods)

array type array name[]={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 numerical types, the default value is also 0

1. Array traversal

Except for the for loop, it is relatively simple Is a 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);
   }
    }

}
Copy after login

The result after running

Ps : Direct traversal method

Java.One-dimensional arrayArrays.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("数组A:"+Arrays.asList(A));
    }
}
Copy after login

2: Array copy

System.arraycopy(a,b,c,d,e) / /a: Source array b: From which position to copy c: Target array d: Offset e: The 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]+" ");
       }
       }
}
Copy after login
Java.One-dimensional arrayThe result after running

3: Filling

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]+" ");
     }
    }

}
Copy after login
Java.One-dimensional arrayAfter 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("数组A和B相等:"+Arrays.equals(A, B));
        System.out.println("数组A和c相等:"+Arrays.equals(A, C));
    }

}
Copy after login
Java.One-dimensional arrayAfter running:

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template