我认为这会将两个数组从资源绑定到一个数组中:
Resource res=getResources();
final int[] one_array=res.getIntArray(R.array.first_array) + res.getIntArray(R.array.second_array);
但是变量数组不能像显示那样声明:
The operator + is undefined for the argument type(s) int[], int[]
另外,我想将资源+一个数组中的两个数组绑定到一个数组中。在我的想法中,它应该是:
Resource res=getResource();
final int[] one_array={ 1,2,3,4,5,res.getIntArray(R.array.first_array),res.getIntArray(R.array.second_array) };
但是变量数组不能像显示那样声明:
Multiple markers at this line
- Type mismatch: cannot convert from
int[] to int
如何通过绑定资源和普通数组中的两个数组来声明一个数组? 是否有其他/替代方法/解决方案来绑定数组?
答案 0 :(得分:4)
final int[] one_array = ArrayUtils.addAll(res.getIntArray(R.array.first_array), res.getIntArray(R.array.second_array);
+
运算符将连接两个字符串。
答案 1 :(得分:0)
或者,如果您不想仅为此操作包含整个jar,请根据addAll()
源代码定义自己的帮助方法。最后,所有它真正做的就是将System.arrayCopy()
两个数组放到一个更大的数组中。
/**
* <p>Adds all the elements of the given arrays into a new array.</p>
* <p>The new array contains all of the element of <code>array1</code> followed
* by all of the elements <code>array2</code>. When an array is returned, it is always
* a new array.</p>
*
* <pre>
* ArrayUtils.addAll(array1, null) = cloned copy of array1
* ArrayUtils.addAll(null, array2) = cloned copy of array2
* ArrayUtils.addAll([], []) = []
* </pre>
*
* @param array1 the first array whose elements are added to the new array.
* @param array2 the second array whose elements are added to the new array.
* @return The new int[] array.
* @since 2.1
*/
public static int[] addAll(int[] array1, int[] array2) {
if (array1 == null) {
return clone(array2);
} else if (array2 == null) {
return clone(array1);
}
int[] joinedArray = new int[array1.length + array2.length];
System.arraycopy(array1, 0, joinedArray, 0, array1.length);
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
return joinedArray;
}
Source(Apache 2.0许可证)。
答案 2 :(得分:0)
也许默认的jdk不使用ArrayUtils类
如果你想使用默认的jdk来绑定数组
使用下面的代码。
int a[] = new int[11];
int b[] = new int[21];
int c[] = new int[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);