Java.一維數組,Java.維數組

來源:互聯網
上載者:User

Java.一維數組,Java.維數組

一位元組的初始化(兩種方法)

數群組類型 數組明[]={值1,值2,值3......值n};

數群組類型 數組名[]=new 資料類型[常量值] //這種方法會給所有的數組元素賦予同一個預設值,對數實值型別來說,預設值也是0

 

1.數組的遍曆

 除了for迴圈,比較簡單的是for-each語句;                 格式:for(類型   變數名:數組)       類型:任何資料類型;數組:定義的數組名

                                                                                  {語句}

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);   }    }}

運行之後的結果

Ps:直接遍曆的方法

Arrays.asList(a)//a是要遍曆的目標數組

 

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));    }}

 

 

2:數組的複製

System.arraycopy(a,b,c,d,e)   //a:源數組 b:從哪個位置開始複製 c:目標數組 d:位移量 e:要從源數組中複製到目標數組元素的個數

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

運行之後的結果

3:填充

Arrays.Fill(a,b)  // a:目標數組名 b:要填充的值

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

運行之後:

4:相等

Arrays.equals(A,B)  //目標數組A 和目標數組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));    }}

運行之後:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.