3

I am trying to convert an ArrayList to ArrayList. I am having actually a list of labels in Double and I want to create a list of Integers. I am trying to add the one to another but of course I need a casting process.

ArrayList<Integer> lab = new ArrayList<Integer>();
lab.addAll(labels.data); //labels.data is an Arraylist of Doubles.

How can I cast one list to another?? I ve tried this to add one by one:

ArrayList<Integer> lab = new ArrayList<Integer>();

     for (int i = 0; i < labels.data.size(); i++) {

            lab.set(i, labels.data.get(i).intValue());

    }

But I received outOfBoundsError.

snake plissken
  • 2,649
  • 10
  • 43
  • 64
  • @snake_plissken Why do this? – Ken Jan 23 '14 at 08:38
  • Iterate on it and build a new `ArrayList`. – Maroun Jan 23 '14 at 08:38
  • 1
    You can't, you have add them one by one. Possible duplicate of [How can I convert ArrayList to ArrayList?](http://stackoverflow.com/questions/4581407/how-can-i-convert-arraylistobject-to-arrayliststring) – BackSlash Jan 23 '14 at 08:38
  • 1
    You can add them one by one using a cast lab.add( labels.get(i).intValue() ) or something like this – Clad Clad Jan 23 '14 at 08:39
  • 1
    You will have to do one by one conversion in loop and add it to list. This should help in conversion. http://stackoverflow.com/questions/9102318/cast-double-to-integer-in-java – RandomQuestion Jan 23 '14 at 08:40
  • Because I ve got several spaghetti functions that works with ArrayList and I think is better done it once in main function. – snake plissken Jan 23 '14 at 08:40
  • you hav to replace set(...) with add(...) since set throws a OutOfBounds Exception. You can use add with an index exactly like you do it with your set method. see also http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#set%28int,%20E%29 – Manuel Manhart Jan 23 '14 at 08:51

4 Answers4

9

You can not convert List<Double> to List<Integer> directly. Loop on each Double object and call intValue() function to get the integer part of it. For e.g. 13.3 will give 13. I hope thats what you want.

for(Double d : labels.data){
    lab.add(d.intValue());
}
RandomQuestion
  • 6,778
  • 17
  • 61
  • 97
  • +1 for me thanks for the answer, but I can't explain the error in my edited code. – snake plissken Jan 23 '14 at 08:44
  • Please don't move the goalposts. This answers your original question. Adding new questions is not helpful. – Jon Kiparsky Jan 23 '14 at 08:47
  • 4
    @snakeplissken, You are receiving error because `set` function is used to replace the value in list, not add to it. Correct function to use is `add`. See http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set(int, E) – RandomQuestion Jan 23 '14 at 08:47
8

First, is there any need for this to be an ArrayList particularly, or for these to be the wrapper classes instead of the primitives? If not, working with a simple array will avoid the overhead of boxing and unboxing, and of storing a lot of objects.

That aside, you'd probably want to loop over the list and cast each item to a double (or a Double), then add it to a new array (or ArrayList). There isn't a bulk operation for this.

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
4

Use Arraylist<Number>. A Number is a parent of both Double and Integer, so you would be able to add Doubles to your list and the Number.intValue() will convert (autoboxing) into Integer when required.

  ArrayList<Number> list;

  list.add(new Double(17.7));
  Integer i = list.get(0).intValue(); // 18, rounding.
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
4

You are getting outOfBoundsError because you are using set() instead of add(). set() is a replacement command and requires there to already be an object in that position.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31