Java 如何将资源数组和普通数组绑定到一个数组中?
我认为这样可以将资源中的两个数组绑定到一个数组中:Java 如何将资源数组和普通数组绑定到一个数组中?,java,android,arrays,android-resources,Java,Android,Arrays,Android Resources,我认为这样可以将资源中的两个数组绑定到一个数组中: 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=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+一个数组绑定到一个数组中。在我看来,应该是:
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
如何通过绑定资源和普通数组中的两个数组来声明一个数组?
是否有其他/替代方法/解决方案绑定阵列?请尝试
+
运算符将连接两个字符串。尝试
+
运算符将包含两个字符串。或者,如果您不想仅为该操作包含整个jar,请根据addAll()
源代码定义您自己的助手方法。最后,它真正做的只是将两个数组合并成一个更大的数组
/**
* <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;
}
*
*@param array1元素添加到新数组的第一个数组。
*@param array2其元素添加到新数组的第二个数组。
*@返回新的int[]数组。
*@自2.1
*/
公共静态int[]addAll(int[]array1,int[]array2){
if(array1==null){
返回克隆(array2);
}else if(array2==null){
返回克隆(array1);
}
int[]joinedaray=新int[array1.length+array2.length];
System.arraycopy(array1,0,joinedArray,0,array1.length);
System.arraycopy(array2,0,joinedArray,array1.length,array2.length);
返回联合阵列;
}
(Apache2.0许可证)。或者,如果您不想仅为此操作包含整个jar,请根据
addAll()
源代码定义您自己的助手方法。最后,它真正做的只是将两个数组合并成一个更大的数组
/**
* <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;
}
*
*@param array1元素添加到新数组的第一个数组。
*@param array2其元素添加到新数组的第二个数组。
*@返回新的int[]数组。
*@自2.1
*/
公共静态int[]addAll(int[]array1,int[]array2){
if(array1==null){
返回克隆(array2);
}else if(array2==null){
返回克隆(array1);
}
int[]joinedaray=新int[array1.length+array2.length];
System.arraycopy(array1,0,joinedArray,0,array1.length);
System.arraycopy(array2,0,joinedArray,array1.length,array2.length);
返回联合阵列;
}
(Apache 2.0许可证)。可能默认jdk不使用ArrayUtils类
如果您想使用默认的jdk绑定数组 使用下面的代码
也许默认jdk不使用ArrayUtils类
如果您想使用默认的jdk绑定数组 使用下面的代码
如果需要三个参数怎么办?我想添加三个数组@MH@InnkuanThute:只需遵循上述相同的模式即可。简而言之,最重要的部分是为第三个数组添加另一个
System.arrayCopy()
调用。更好的是:使该方法在任意数量的数组上工作。我将把它作为家庭作业练习留给你如果需要三个参数怎么办?我想添加三个数组@MH@InnkuanThute:只需遵循上述相同的模式即可。简而言之,最重要的部分是为第三个数组添加另一个System.arrayCopy()
调用。更好的是:使该方法在任意数量的数组上工作。我将把它作为家庭作业练习留给你
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);