標題:
陣列 (一)
[打印本頁]
作者:
鄭繼威
時間:
2023-6-7 14:40
標題:
陣列 (一)
「陣列(Array)」
,
按序
排列的
同類
資料元素的
集合
。
1.
索引值是從0
開始計數
2.索引值
不可超出陣列的範圍
3.取陣列最後一個值=陣列長度-1
分別運用四種不同的陣列宣告方式, 完成參考執行畫面.
public class Ch34
{
public static void main(String args[])
{
String a[]={"春","夏","秋","冬"};
String b[]={"甲","乙","丙","丁"};
System.out.print("陣列a: ");
for(int i=0; i<4; i++)
System.out.print(a[i]+" ");
System.out.println();
System.out.print("陣列b: ");
for(int i=0; i<4; i++)
System.out.print(b[i]+" ");
System.out.println();
int c[]={1,2,3,4};
//string d[4]; -> c++宣告長度為4的空陣列
String d[]=new String[4]; //宣告長度為4的空陣列
d[0]="A";
d[1]="B";
d[2]="C";
d[3]="D";
//foreach
//for(陣列型態 變數:陣列(來源))
System.out.print("陣列c: ");
for(int i:c)
System.out.print(i+" ");
System.out.println();
System.out.print("陣列d: ");
for(String sa:d)
System.out.print(sa+" ");
System.out.println();
}
}
複製代碼
作者:
林劭澧
時間:
2023-6-11 20:36
public class Ch01
{
public static void main(String args[])
{
String a[]={"春","夏","秋","冬"};
String b[]={"甲","乙","丙","丁"};
System.out.print("陣列a: ");
for(int i=0; i<4; i++)
System.out.print(a[i]+" ");
System.out.println();
System.out.print("陣列b: ");
for(int i=0; i<4; i++)
System.out.print(b[i]+" ");
System.out.println();
int c[]={1,2,3,4};
String d[]=new String[4];
d[0]="A";
d[1]="B";
d[2]="C";
d[3]="D";
System.out.print("陣列c: ");
for(int i:c)
System.out.print(i+" ");
System.out.println();
System.out.print("陣列d: ");
for(String sa:d)
System.out.print(sa+" ");
System.out.println();
}
}
複製代碼
作者:
黃裕恩
時間:
2023-6-13 11:35
public class Pojjn
{
public static void main(String args[])
{
String a[]={"春","夏","秋","冬"};
String b[]={"甲","乙","丙","丁"};
System.out.print("陣列a: ");
for(int i=0; i<4; i++)
System.out.print(a[i]+" ");
System.out.println();
System.out.print("陣列b: ");
for(int i=0; i<4; i++)
System.out.print(b[i]+" ");
System.out.println();
int c[]={1,2,3,4};
String d[]=new String[4];
d[0]="A";
d[1]="B";
d[2]="C";
d[3]="D";
System.out.print("陣列c: ");
for(int i:c)
System.out.print(i+" ");
System.out.println();
System.out.print("陣列d: ");
for(String sa:d)
System.out.print(sa+" ");
System.out.println();
}
}
複製代碼
作者:
李彣
時間:
2023-6-14 18:26
public class B
{
public static void main(String args[])
{
String a[]={"春","夏","秋","冬"};
String b[]={"甲","乙","丙","丁"};
System.out.print("a:");
for(int i=0; i<4; i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("b:");
for(int i=0; i<4; i++)
{
System.out.print(b[i]+" ");
}
System.out.println();
int c[]={1,2,3,4};
String d[]=new String[4];
d[0]="A";
d[1]="B";
d[2]="C";
d[3]="D";
System.out.print("c:");
for(int i:c)
{
System.out.print(i+" ");
}
System.out.println();
System.out.print("d:");
for(String sa:d)
{
System.out.print(sa+" ");
}
System.out.println();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2