一維陣列
陣列可以存放相同的資料型態資料,並利用索引值存取其內容。
一、宣告方式
必須先宣告陣列後配置記憶體位置給陣列。
資料型態 陣列名稱[];
陣列名稱=new 資料型態[數量];int score[]; //宣告整數陣列score
score=new int[5]; //使陣列score 可存放5個整數的記憶體空間較簡潔的方式:
資料型態 陣列名稱[]=new 資料型態[數量]; int score[ ]; 還有另外的宣告方式:
int[] score;
int []score;二、取得陣列長度 length
public class ch06_1 {
public static void main(String[] args) {
int i;
int a[];
a=new int[3];
a[0]=9;
a[1]=6;
for(i=0; i<a.length; i++)
System.out.print("a["+i+"]="+a[i]+",\t");
System.out.println("字串長度:"+a.length);
}OUTPUT: a[0]=9, a[1]=6, a[2]=0, 字串長度:3
三、陣列設初值
OUTPUT: a[0]=10 a[1]=20 a[2]=30 字串長度:3
四、尋找陣列中最大最小值
OUTPUT: 22 15 89 74 3 max= 89 min= 3
Last updated
Was this helpful?