[英]How to assign int value to integer variable
How can I convert an int value to integer value? 如何将int值转换为整数值?
Here is my code- 这是我的代码-
private static int[] value= {R.drawable.collection1,R.drawable.collection2}
public static ArrayList<Integer> AddIntValue (int[] value){
ArrayList<Integer> Intvalue=new ArrayList<Integer>();
for(int i=0;i<value.length; i++)
{
Intvalue.add(Integer.valueOf(value[i]), null);
}
return Intvalue;
}
Am getting error on Intvalue.add(Integer.valueOf(value[i]), null); 在Intvalue.add(Integer.valueOf(value [i]),null)上出错。
Please help me 请帮我
Thanks 谢谢
java 5 added autoboxing, so you should just be able to use this Java 5添加了自动装箱功能,因此您应该可以使用它
int i=3;
Integer number=i;//number now equals 3
The reason you are getting an error is because you are using the array for indices and trying to add null at this indices. 出现错误的原因是因为您使用数组作为索引,并试图在此索引处添加null。 If you array was {100,101,102}, It would try to add null to intValues at index 100, 101, and 102, which don't exist, giving you the IndexOutOfBoundsEception;
如果您的数组为{100,101,102},它将尝试在索引100、101和102处的intValues中添加null(不存在),从而为您提供IndexOutOfBoundsEception;
If you want to use your code and still has control over elements list index, you have to: 1. Initzialize ArrayList size 2. Use correctly add method 如果要使用代码并且仍然可以控制元素列表索引,则必须:1.初始化ArrayList的大小2.使用正确的add方法
public static List<Integer> addIntValue(int[] value) {
List<Integer> intValues = new ArrayList<Integer>(value.length);
for (int i = 0; i < value.length; i++) {
intValues.add(i, value[i]);
}
return intValues;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.