java數(shù)組
package com.mage.array;
/*
* 數(shù)組:
* 1:什么數(shù)組
* a: 一組數(shù)
* b:一 組 在內存中存儲的是連續(xù)的空間 具備(相同數(shù)據(jù)類型) 的 數(shù)據(jù)的 (有序)集合脖岛。
*
*
* 2:在java中如何使用數(shù)組
* 數(shù)組的聲明:
* eg:數(shù)據(jù)類型[] 變量名 ; int[] arrs;
*
* 靜態(tài)賦值
* 聲明和賦值分開
* arrs = new int[] {1,1,2,3,5};
* 變量名 = new 數(shù)據(jù)類型[]{元素1,元素2...};
* 可以指定數(shù)組的元素個數(shù)以及數(shù)組的元素內容
* 聲明和賦值在一起
* String[] strs = {"嘿嘿","哈哈"};
String[] strs = new String[] {"",""};
* 動態(tài)賦值
* int[] arr = new int[5];
* 只規(guī)定存儲元素的個數(shù) 不規(guī)定值
* 常見屬性:
* length: 查看數(shù)組的長度 數(shù)組.length
* 索引:數(shù)組中的每個元素都存在一個具體的索引值,
* 索引值是從0開始,到數(shù)組.slength-1結束
*
* 為什么數(shù)組的索引是從0開始的?
*
* 重點: 數(shù)組的長度一旦聲明是不可變的垒迂。
*
* 數(shù)組的使用場景:
* 數(shù)組通過索引去隨機獲取元素的效率高缸榄、通過索引去修改元素效率高
* 通過元素值修改赵讯、刪除碳蛋、增加效率低
*/
public class Test01 {
public static void main(String[] args) {
//聲明一個數(shù)組
int[] arrs;
//對于數(shù)組進行賦值
arrs = new int[] {1,1,2,3,5};
//聲明一個數(shù)組
//String[] strs = new String[] {"",""};
String[] strs = {"嘿嘿","哈哈"};
//聲明一個數(shù)組
int[] arr = new int[5];
//聲明一個數(shù)組
int[] strs = new int[10];
System.out.println("查看數(shù)組的元素:"+strs.length);
//查看某一個元素
int index1 = strs[0];
System.out.println(index1);
strs[0] = 10;//將10賦值給strs數(shù)組的第0個位置的元素
System.out.println(strs[0]);
strs = new int[20];
System.out.println(strs.length);
}
}