比方说,我有这样的方法:如何在Java中添加两个数组每个迭代,然后保存数组的内容
public byte[] generateCoefficientsOfPolynomials(int k ,byte [] file,Random rnd) {
for (int i=0;i<file.length;i++) {
// for each iteration it creates a vector of size 3
}
// some other code here
// here we call the method below where allRndCoefficients is each
// array taken from this method for instance [12,13,-20] and so on
con.sumOfCoefficients(allRndCoefficients, k);
// return type here
}
我会从这个方法得到的将是像那些阵列: 对于第一次迭代[12,13,-20]
,对于第二次迭代[123,-25,-56]
等等。
注意:由于我已经使用了rnd.nextBytes(k);
,这些数组具有随机值,其中k
是数组中元素的数量。
现在我想实现的是将这些数组逐个添加到一起。假设[12,13,-20]与[123,-25,56]一起添加并保存结果。然后我们保存的这个结果我们想要添加上面方法的第三次迭代产生的数组。
我写了下面的代码来实现这一点:
public byte[] sumOfCoefficients(byte [] temp,int k) {
galoaField256 d = new galoaField256(); // create new reference
d.setGeneratorPolynom(3);
d.getGeneratorPolynom();
d.setString("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
d.getString();
d.loadExp();
d.loadLog();
byte [] initialSum=new byte [k];
for(int i=0;i<k;i++) {
initialSum[i]=(byte)d.add(initialSum[i],temp[i]);
}
System.out.println("The sum is" +Arrays.toString(initialSum));
return initialSum;
}
其中byte[] temp
是我们的第一个方法generateCoefficientsOfPolynomials
的每次迭代之后采取阵列。
不幸的是,两个向量的求和不在这里完成。我得到的正是由第一种方法产生的数组,意味着每次我们添加从第一种方法获取的数组时,由0个元素组成的数组,因此initialSum
数组不会为第一种方法的每次迭代保存其状态。任何人都可以拯救我的一天,并告诉我我的代码有什么问题?
题外话:请考虑使用[驼峰](https://sanaulla.info/2008/06/25/camelcase -notation-naming-convention-for-programming-languages /)符号。此外,您正在使用哪个[API](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html)?为什么你使用**字节**而不是**整数**? –