2

How can I convert an int value to integer value?

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);

Please help me

Thanks

3
  • 3
    You arraylist is empty, so adding at a specific position (which does not exist yet) will throw an exception. What are you trying to achieve? Commented Nov 17, 2012 at 12:15
  • No.I tryed Integer k=Integer.valueOf(value[i]); before Intvalue.add(Integer.valueOf(value[i]), null); then i found that K is not null k have value. Commented Nov 17, 2012 at 12:27
  • 2
    looking at one of your previous comments, you seem to mix R.integer and Integer. You should clarify what you want (in other words, what do you want the list to contain after that method). Commented Nov 17, 2012 at 12:47

2 Answers 2

5

java 5 added autoboxing, so you should just be able to use this

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. 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;

Sign up to request clarification or add additional context in comments.

Comments

0

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

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.