資料結構(data stracture)
陣列
定義 (非正式) 一群相同型態的變數藉由陣列加以群組化
語法 資料型態名稱 [] 陣列名稱
例如:int [] scores;
初始化(兩種方法)
1.一宣告就給初值 int[] scroes = {52.68.75.83.36}
2. 先宣告空間再給初值 int[] scores = new int[5] ;
scores[0]= ;
scores[1]= ;
scores[2]= ;
scores[3]= ;
scores[4]= ;
注意事項:陣列超用(常犯錯部分)
超出陣列宣告的初值,故可以將方法改為索引長度的設定
例如
宣告 int[] scores = new int[5] ;
但超過使用配置 int[6](錯!!最高應為[4])
解決方法:
int [] scores = {52..68.75.83.36}
for (int i =0; i<scores.length; i=i+1)
二維陣列
語法 int [][] tictac toy
井字遊戲 以二維陣列表示之方法有二
方法一
int [][] tic = new int[3][3];
{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}}
方法二
int[][]tic= new int[3][3];
給初始值為-1
for (int i =0 ; i<=2; i=i+1)
for (int j=0; j<=2;j=j+1)
tic[i][j]=-1;
類別方法
是一種模組的概念一種函數的概念可將重複出現的程式碼以類別方法加以整理歸納
沒有留言:
張貼留言