Java &引用;ArrayIndexOutOfBoundsException“;尝试添加两个int数组时出错
我试图在Java中实现“添加”两个数组的元素。 我有两个包含整数的数组,我想把它们相加。我不想使用不可变的变量。我宁愿做那样的事:a+b; 问题是当我添加两个长度不同的数组时,它试图将b的元素添加到a,但如果b的长度更大,它会标记错误“ArrayIndexOutOfBoundsException”。 我能理解为什么会这样。但我怎么才能解决这个问题呢? 如何展开阵列a?:/Java &引用;ArrayIndexOutOfBoundsException“;尝试添加两个int数组时出错,java,arrays,Java,Arrays,我试图在Java中实现“添加”两个数组的元素。 我有两个包含整数的数组,我想把它们相加。我不想使用不可变的变量。我宁愿做那样的事:a+b; 问题是当我添加两个长度不同的数组时,它试图将b的元素添加到a,但如果b的长度更大,它会标记错误“ArrayIndexOutOfBoundsException”。 我能理解为什么会这样。但我怎么才能解决这个问题呢? 如何展开阵列a?:/ public void plus(int[] b) { int maxlength = Math.
public void plus(int[] b)
{
int maxlength = Math.max( this.length, b.length );
if (maxlength==a.length)
{
for (int i = 0; i <= maxlength; i++)
{
a[i] = a[i] + b[i]; //ArrayIndexOutOfBoundsException error
}
}
}
public void plus(int[]b)
{
int maxlength=Math.max(this.length,b.length);
如果(maxlength==a.length)
{
对于(int i=0;ii请尝试替换:
for (int i = 0; i <= maxlength; i++)
for(int i=0;imaxlength是a[]和b[]大小之间的最大值,因此在从0到maxlength的循环中,当i超过a[]和b[]大小的min时,您将获得ArrayIndexOutOfBoundsException
试试这个:
public void plus(int[] b)
{
Polynomial a = this;
int[] c;
int maxlength;
if (a.length>b.length) {
c=a;
maxlength=a.length;
} else {
c=b;
maxlength=b.length;
}
int ca, cb;
for (int i = 0; i < maxlength; i++)
{
if (i<this.length)
ca=a[i];
else
ca=0;
if (i<b.length)
cb=b[i];
else
cb=0;
c[i] = ca + cb;
}
}
public void plus(int[]b)
{
多项式a=这个;
int[]c;
int最大长度;
如果(a.长度>b.长度){
c=a;
maxlength=a.length;
}否则{
c=b;
maxlength=b.长度;
}
int-ca,cb;
对于(int i=0;i
如果您需要可变大小的数据结构,请不要使用数组。这样如何:
private int[] a;
/**
* Adds the specified array to our array, element by element, i.e.
* for index i, a[i] = a[i] + b[i]. If the incoming array is
* longer, we pad our array with 0's to match the length of b[].
* If our array is longer, then only the first [b.length] values
* of our array have b[] values added to them (which is the same
* as if b[] were padded with 0's to match the length of a[].
*
* @param b the array to add, may not be null
*/
public void plus(final int[] b)
{
assert b != null;
if (a.length < b.length) {
// Expand a to match b
// Have to move a to a larger array, no way to increase its
// length "dynamically", i.e. in place.
final int[] newA = new int[b.length];
System.arraycopy(a, 0, newA, 0, a.length);
// remaining new elements of newA default to 0
a = newA;
}
for (int i = 0; i < b.length; i++)
{
a[i] = a[i] + b[i];
}
}
但是如果是一个更大的数组,那么它不会将其最后一个元素添加到a:/只是尝试检查一下..如果b的最后一个元素大于a,那么它不会添加b的最后一个元素。编辑:我也尝试使用min,但是我如何才能添加更大数组的其余元素?这就是为什么我要求扩展arrays@FILIaS-你不能这样做…现在你正在分配added值返回到a。如果“b”大于“a”,并且您希望将b中的最后一个元素相加……您将在哪里存储它们?我建议您在您的案例中使用ArrayList而不是array。@FILIaS-使用ArrayList处理通用解决方案,很快就会返回。@FILIaS-给出了一个代码段,它将元素相加,而不考虑a和b的长度。或者有人趁我不注意的时候偷偷将运算符重载潜入Java,或者这段代码不对-a是一个polymonent
类型的变量,这是一个用户定义的类,但您使用的是a[I]
。可能的重复我已经使用另一个帮助数组正确地完成了。但我不想这样做。我只想更改数组a。thanxI更改了它,所以c只是对a或b的引用。但即使现在,你也不更改数组。是的,我更改了一个。例如,如果a是最长的数组,c将是对同一数组的引用,而这个数组将在每个循环中更改。写入的时间更短。您可以使用一个条件而不是行c[i]=ca+cb;,但它只会更长更复杂,以获得相同的结果。嗯,我认为:如果(maxlength==a.Length)不需要正确?同样在这一点上:cb=b[0];您的意思是cb=b[i]?我已经考虑过了,问题是我在使用varargs的方法中获取数组作为参数。比如:getArray(int…arrayA){…}所以我不知道如何将int数组传输到linkedList@FILIaS:,或者自己写(不难)我认为对于基元数组来说并不是那么容易。没错!我是在参考你关于在基元数组中使用arrays.asList()的建议,当OP的varargs参数的类型为“int”时,非常感谢BertF!关于这个:System.arraycopy(a,0,newA,0,a.length);你会说更好的System.arraycopy(a,0,newA,0,b.length)如果我理解得很好,那就是你的意思,对吗?不,a.length是正确的。我们想创建一个新数组newA
,即b.length
long,但我们想复制旧的a[]
数据(在a[]中有确切的a.length
项)对于新数组的第一个a.length
空格newA
。啊哈,好吧!另一个注意事项是:for(int i=0;ia[]={1,2,3},
b[]={1,2}`-我只想把2(min(2,3)
)个值加起来(因为b
没有第三个值。如果a[]={1,2,3},
b[]={1,2,3,4}我只想把3(min(3,4)
)个值加起来怎么样(因为a
没有第四个值。但是,我已经确保(a.length>=b.length)
(如果a
之前短了,那么如果代码已经变长了)。因此,当(a.length>=b.length)
时,那么min(a.length,b.length)==b.length
。用这个测试我的代码得到:如果a[]={1,2,3},b[]={1,2,3,4}…a+b={2,4,6}:/正确的不应该是{2,4,6,4}?
public void plus(int[] b)
{
Polynomial a = this;
int[] c;
int maxlength;
if (a.length>b.length) {
c=a;
maxlength=a.length;
} else {
c=b;
maxlength=b.length;
}
int ca, cb;
for (int i = 0; i < maxlength; i++)
{
if (i<this.length)
ca=a[i];
else
ca=0;
if (i<b.length)
cb=b[i];
else
cb=0;
c[i] = ca + cb;
}
}
private int[] a;
/**
* Adds the specified array to our array, element by element, i.e.
* for index i, a[i] = a[i] + b[i]. If the incoming array is
* longer, we pad our array with 0's to match the length of b[].
* If our array is longer, then only the first [b.length] values
* of our array have b[] values added to them (which is the same
* as if b[] were padded with 0's to match the length of a[].
*
* @param b the array to add, may not be null
*/
public void plus(final int[] b)
{
assert b != null;
if (a.length < b.length) {
// Expand a to match b
// Have to move a to a larger array, no way to increase its
// length "dynamically", i.e. in place.
final int[] newA = new int[b.length];
System.arraycopy(a, 0, newA, 0, a.length);
// remaining new elements of newA default to 0
a = newA;
}
for (int i = 0; i < b.length; i++)
{
a[i] = a[i] + b[i];
}
}
private ArrayList<Integer> aList;
public void plusList(final int[] b)
{
assert b != null;
if (aList.size() < b.length) {
aList.ensureCapacity(b.length);
}
for (int i = 0; i < b.length; i++)
{
if (i < aList.size()) {
aList.set(i, aList.get(i) + b[i]);
} else {
aList.add(b[i]);
}
}
}
public class AddableArray {
private int[] a;
public AddableArray(final int... a) {
this.a = a;
}
/**
* Adds the specified array to our array, element by element, i.e.
* for index i, a[i] = a[i] + b[i]. If the incoming array is
* longer, we pad our array with 0's to match the length of b[].
* If our array is longer, then only the first [b.length] values
* of our array have b[] values added to them (which is the same
* as if b[] were padded with 0's to match the length of a[].
*
* @param b the array to add, may not be null
*/
public void plus(final int[] b)
{
assert b != null;
if (a.length < b.length) {
// Expand a to match b
// Have to move a to a larger array, no way to increase its
// length "dynamically", i.e. in place.
final int[] newA = new int[b.length];
System.arraycopy(a, 0, newA, 0, a.length);
// remaining new elements of newA default to 0
a = newA;
}
for (int i = 0; i < b.length; i++)
{
a[i] = a[i] + b[i];
}
}
int[] get() {
return a;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("a[] = [ ");
for (int i = 0; i < a.length; i++) {
if (i > 0) sb.append(", ");
sb.append(a[i]);
}
sb.append(" ]");
return sb.toString();
}
public static void main (final String[] args) {
final AddableArray myAddableArray = new AddableArray(1,2,3);
System.out.println("Elements before plus(): ");
System.out.println(myAddableArray.toString());
final int b[]={1,2,3,4};
myAddableArray.plus(b);
System.out.println("Elements after plus(): ");
System.out.println(myAddableArray.toString());
}
}
Elements before plus():
a[] = [ 1, 2, 3 ]
Elements after plus():
a[] = [ 2, 4, 6, 4 ]