日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

2013.8.7Java语言基础——数组

發(fā)布時間:2023/12/1 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2013.8.7Java语言基础——数组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  數(shù)組是數(shù)據(jù)類型一致的變量的集合。

一個:變量

一堆(多個):數(shù)組

數(shù)組語法:

1)數(shù)組變量(引用類型變量)

  數(shù)組變量通過引用地址引用了數(shù)組(數(shù)組對象)

2)數(shù)組(數(shù)組對象)

  數(shù)組就是個體,是一個整體

  數(shù)組元素的個數(shù)叫數(shù)組的長度length

  數(shù)組元素的范圍:0 1 2…length-1

3)數(shù)組元素

  是數(shù)組中的每個變量,使用[下標]訪問

  • 定義數(shù)組變量;
  • 創(chuàng)建數(shù)組(數(shù)組對象)
  • 訪問數(shù)組元素?
  • 1 public class Test { 2 3 public static void main(String[] args) { 4 //1)定義數(shù)組變量 5 int[] ary;//聲明int[] 數(shù)組類型的變量ary 6 int ary1;//也是聲明數(shù)組變量,但很少使用!!! 7 //System.out.println(ary[0]);//編譯錯誤,ary沒有初始化 8 ary = null;//null 空,沒有 9 //System.out.println(ary[0]);//運行異常,空指針異常 10 11 //空指針異常發(fā)生原因:引用類型的變量值是null,不引用任何對象 12 //當利用引用變量訪問屬性(數(shù)組元素)和方法時,出現(xiàn)空指針異常 13 14 //2)創(chuàng)建數(shù)組(創(chuàng)建 數(shù)組對象) 15 ary = new int[3];//數(shù)組對象有3個元素 16 //創(chuàng)建數(shù)組對象(new int[3]),將對象的引用賦值給ary變量 17 //ary就引用了數(shù)組對象,這個對象有3個元素 18 System.out.println(ary[0]);//0 19 //System.out.println(ary[3]);//運行異常,數(shù)組下標越界 20 //java數(shù)組元素是自動初始化的,初始值為零值,各種零值如下: 21 //0 0.0 \u0000 null false 22 double[] d = new double[3]; 23 System.out.println("d[0] = "+d[0]);//d[0] = 0.0 24 char[] c = new char[3]; 25 System.out.println("c[0] = "+c[0]);//c[0] = (看不出顯示什么) 26 System.out.println((int)'\u0000');//\u0000的編碼值是0 27 String[] s = new String[3]; 28 System.out.println("s[0] = "+s[0]);//s[0] = null(字符串數(shù)組初始化是null) 29 boolean[] b = new boolean[3]; 30 System.out.println("b[0] = "+b[0]);//b[0] = false 31 32 //java中數(shù)組創(chuàng)建的3種方式 33 //new 類型[長度] 34 //類型是任何類型:基本類型、類類型(String,Integer) 35 //長度:變量或常量值:0~Integer.MAX_VALUE 36 37 //new 類型[]{元素0,元素1,元素2} 38 //直接給出元素,元素直接初始化,元素的個數(shù)就是長度 39 ary =new int[]{2,3}; 40 System.out.println(ary[0]);//2 41 42 //靜態(tài)初始化 43 //類型[] 變量={元素0,元素1,元素2} 44 int[] ary4 = {2,3,4}; 45 //靜態(tài)初始化“只能用于”聲明變量同時初始化! 46 //ary = {4,5,6}//編譯錯誤 47 ary = new int[]{4,5,6};//沒有問題 48 49 //new int[length]適合創(chuàng)建不知道具體的元素,數(shù)量很多 50 //new int[]{2,3,4}適合已經(jīng)知道具體元素,元素比較少 51 //{4,5,6}只適合知道元素,并只能使用在聲明變量時直接初始化 52 53 //3)訪問數(shù)組元素 54 //數(shù)組元素一旦創(chuàng)建,數(shù)組的長度是不可以改變的 55 //使用.length屬性可以獲取數(shù)組的長度 56 //數(shù)組不能越界訪問,會出現(xiàn)運行異常 57 ary = new int[]{5,6,7}; 58 System.out.println(ary.length); 59 ary[1]++; 60 System.out.println(ary[1]);//7 61 System.out.println(ary[-1]);//運行異常!下標越界 62 //迭代數(shù)組元素,迭代數(shù)組也稱為遍歷數(shù)組,就是逐一處理元素 63 for(int i=0;i<ary.length;i++){ 64 System.out.println(ary[i]); 65 } 66 } 67 }

    使用數(shù)組非常有用的API方法

    (使用API方法:API方法封裝了常用算法功能,使用這些功能簡化開發(fā)過程,提高開發(fā)效率)

      Arrays.toString:用于字符串表示數(shù)組元素

    1 package TestCode; 2 3 import java.util.Arrays; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 String[] str =new String[]{"A","b","c","d"}; 9 System.out.println(Arrays.toString(str));//打印出數(shù)組元素[A, b, c, d] 10 }

      Arrays.equals:用于比較兩個數(shù)組內(nèi)容是否相等(便捷)

    1 package TestCode; 2 3 import java.util.Arrays; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 char[] a1 = new char[]{'a','b','c'}; 9 char[] a2 = new char[]{'a','b','c'}; 10 char[] a3 = new char[]{'a','c','d'}; 11 System.out.println(Arrays.equals(a1, a2));//true 12 System.out.println(Arrays.equals(a1, a3));//false 13 } 14 }

      Arrays.sort:用于實現(xiàn)數(shù)組排序

    1 package TestCode; 2 3 import java.util.Arrays; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 char[] a = new char[]{'a','d','f','c','b','g'}; 9 System.out.println(Arrays.toString(a));//[a, d, f, c, b, g] 10 Arrays.sort(a); 11 System.out.println(Arrays.toString(a));//[a, b, c, d, f, g] 12 } 13 }

      Arrays.binarySearch:用于實現(xiàn)有序數(shù)組 的二分法查找

    只能查找排序后的數(shù)組,不然得到的結果不穩(wěn)定

    1 package TestCode; 2 3 import java.util.Arrays; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 char[] a = new char[]{'a','d','f','c','b','g'}; 9 int index= Arrays.binarySearch(a, 'c'); 10 System.out.println(index);//-2負數(shù)找不到 11 Arrays.sort(a); 12 index= Arrays.binarySearch(a, 'c'); 13 System.out.println(index);//在排序后數(shù)組的第2位 14 } 15 }

    ?數(shù)組的復制

    最常見用途:數(shù)組的擴容算法

    1 package TestCode; 2 3 import java.util.Arrays; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 int[] ary1 = {4,5,6}; 9 int[] ary2 = ary1;//數(shù)組變量的賦值,但數(shù)組還是同一個。ary1 ary2互相影響 10 ary2[1]++; 11 System.out.println(Arrays.toString(ary1));//[4, 6, 6] 12 System.out.println(Arrays.toString(ary1));//[4, 6, 6] 13 //數(shù)組的復制:1)使用for循環(huán)實現(xiàn) 2)使用API 14 //將ary1引用數(shù)組進行復制 15 int[] ary3 = new int[ary1.length]; 16 for(int i=0;i<ary1.length;i++){ 17 ary3[i] = ary1[i]; 18 } 19 ary3[1]++; 20 System.out.println(Arrays.toString(ary3));//[4, 7, 6] 21 System.out.println(Arrays.toString(ary1));//[4, 6, 6] 22 //使用API System.arraycopy()實現(xiàn)復制,比較早期,用C寫的,性能好 23 int[] ary4 = new int[ary1.length]; 24 //參數(shù):源數(shù)組,源數(shù)組位置,目標數(shù)組,目標數(shù)組位置,個數(shù) 25 System.arraycopy(ary1, 0, ary4, 0, ary1.length); 26 System.out.println(Arrays.toString(ary1)); 27 System.out.println(Arrays.toString(ary4)); 28 //使用Arrays.copyOf()方法(JDK1.5開始出現(xiàn)),底層就是arraycopy 29 int[] ary5 = Arrays.copyOf(ary1, ary1.length); 30 System.out.println(Arrays.toString(ary1)); 31 System.out.println(Arrays.toString(ary5)); 32 33 } 34 }

    數(shù)組的擴容 與 追加

    1) 數(shù)組創(chuàng)建以后長度不可改變
    2) 利用更換數(shù)組的方式實現(xiàn)擴容算法
    3) 更換數(shù)組時候,利用復制方法保持原數(shù)組內(nèi)容。

    ?數(shù)組擴容是Java API 實現(xiàn)的常用算法!

    ?

    1 package TestCode; 2 3 import java.util.Arrays; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 // 擴容原理:更換新數(shù)組 9 String[] playlist = { "song1", "song2" }; 10 System.out.println(Arrays.toString(playlist)); 11 playlist = new String[] { "song1", "song2", "song3" }; 12 System.out.println(Arrays.toString(playlist)); 13 // 擴容:為playlist擴容并追加 新 歌曲 “song4” 14 playlist = Arrays.copyOf(playlist, playlist.length + 1); 15 playlist[playlist.length - 1] = "song4"; 16 System.out.println(Arrays.toString(playlist)); 17 // 1) 復制新數(shù)組(增加容量) 18 // 2) 替換原數(shù)組 19 String[] newOne = new String[playlist.length + 1]; 20 System.arraycopy(playlist, 0, newOne, 0, playlist.length); 21 playlist = newOne; 22 System.out.println(Arrays.toString(playlist)); 23 } 24 }

    ?

    ?

    ?

    ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/grape/p/3244159.html

    總結

    以上是生活随笔為你收集整理的2013.8.7Java语言基础——数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。