# Java陣列的遍歷與求和知識點

``````
public class OneArry{
public static void main(String[] args) {
double[] num = {1.9, 2.9, 3.4, 3.5,10,-1};
num[0]=new Double(2); //通過下標把1.9變為2.0
double sum = 0;
for (int i = 0; i < num.length; i  ) { //遍歷
System.out.println(num[i]);
sum =num[i]; //求和
}
System.out.println("一維陣列之和為 "   sum);
}
}``````

``````
2.0
2.9
3.4
3.5
10.0
-1.0

``````
public class TwoArry{
public static void main(String[] args) {
int sum=0;
int num [][] ={
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}} ;
System.out.println("輸出二維陣列:");
num[0][0]=new Integer(100); //通過下標把陣列裡的1變為100
for(int i=0;i<num.length;i  ){ //遍歷，排列
for( int j=0;j<num[i].length;j  ){
System.out.print(num[i][j] "\t");
sum =num[i][j]; //求和
}
System.out.println();
}
System.out.println("二維陣列之和為：" sum);
}
}``````

``````

100	2	3	4	5
6	7	8	9	10
11	12	13	14	15
16	17	18	19	20
21	22	23	24	25

``````
public class TwoArry{
public static void main(String args[ ]){
int[][]num=new int[5][5];
int count=1; //陣列從1開始迴圈
int sum=0;
for(int i=0;i<num.length;i  ){
for(int j=0;j<num[i].length;j  ){
num[i][j]=count  ; //count新增到陣列裡
sum =num[i][j]; //求和
//	        System.out.println(num[i][j]); //列印出所有陣列
}
}
System.out.println("二維陣列之和為：" sum);
System.out.println("輸出二維陣列:");
for(int i=0;i<num.length;i  ){ //進行排列
for(int j=0;j<num[i].length;j  ){
System.out.print(num[i][j] "\t");
}
System.out.println();
}
}
} ``````

``````

1	2	3	4	5
6	7	8	9	10
11	12	13	14	15
16	17	18	19	20
21	22	23	24	25``````