求大整数和时,为啥会报数组越界异常?
课程设计
1
package com.seqlist;
public class SeqList {
/**
* @java.lang.ArrayIndexOutOfBoundsException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] Adata={3,2,5,1,2,3,4,3};
int [] Bdata={6,4,3,2,4,5,6};
int flag=0;
int n=Adata.length;
int m=Bdata.length;
int [] Cdata={};
int i=0;
int x=Cdata.length;
while(i<n&&i<m){
//数组越界,为什么为什么为什么????
Cdata[i]=(Adata[i]+Bdata[i]+flag)%10;
flag=(Adata[i]+Bdata[i]+flag)/10;
i++;
}
for(;i<n;i++){
Cdata[i]=(Adata[i]+flag)%10;
flag=(Adata[i]+flag)/10;
}
for(;i<m;i++){
Cdata[i]=(Bdata[i]+flag)%10;
flag=(Bdata[i]+flag)/10;
}
x =Math.max(m,n)+flag;
if(flag==1)Cdata[x-1]=1;
for(int j=0;j<Cdata.length-1;j++) {
System.out.print(Cdata[j]);
}
}
}
发表回复