0

I am trying to create an flexible list of ints in java. By flexible I mean one which its length can be changed. It cant be of type "Integer" it must be of type "int". However you cannot use an ArrayList because it does not accept primitive types. So is there any way to do this?

Why I need this is because while my program is running I need to add ints to a list where I will later convert to an array to be used in the constructor of an object.

ManOfPanda
  • 35
  • 7
  • 1
    http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem. What is your real question? – Jayan Aug 21 '14 at 04:59
  • Why can't it be `Integer`? – jdphenix Aug 21 '14 at 05:00
  • possible duplicate of [Why can Java Collections not directly store Primitives types?](http://stackoverflow.com/questions/2504959/why-can-java-collections-not-directly-store-primitives-types) – Jayan Aug 21 '14 at 05:01
  • You could always implement a linked list yourself although I don't understand why you don't want to use LinkedList. – alfredaday Aug 21 '14 at 05:02
  • @alfredaday Relatively minor thing, but ArrayList _tends_ to be a better default choice than LinkedList. It can be more memory efficient and it has better memory locality, which means it tends to work better with CPU caches. It also has random access, of course. Those benefits often outweigh the costs of having to copy the array when you exceed capacity. (As with pretty much everything in CS, these are general guidelines, _not_ hard and fast rules.) – yshavit Aug 21 '14 at 05:14
  • Agreed. He said "flexible" and I understood that as needing a dynamically sized array, in which case LinkedList would be best. Otherwise ArrayList is definitely a good default choice. – alfredaday Aug 21 '14 at 05:23
  • @alfredaday no, it is not enough for LinkedList to be a better choice than ArrayList. LinkedList should be used *extremely* rarely, *only* when you remove from or add elements in the middle of the list *via ListIterator methods*. – leventov Aug 22 '14 at 13:01

1 Answers1

3

In Java you can not create an ArrayList of primitive types (say, int) because Java's collections API only works for object types. But declaring an ArayList<Integer> is perfectly valid, and in fact, the right choice for this problem.

Later on, if you need to create an array, just call toArray(), to obtain an Integer[]. If necessary, you could manually copy the contents of the Integer[] to an int[].

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Well ok, i found a way arround my problem but it still seems tedious you have to go through this way. – ManOfPanda Aug 21 '14 at 05:04
  • 1
    That issue is because `ArrayList<>().remove(int index);` would be extremely conflicting and confusing with `ArrayList<>().remove(Object object)` (if 'object' happened to be a primitive int). In C# by the other hand, there are 2 different methods to the 2 different approaches (other than an overload), which are `Remove(object)` and `RemoveAt(index)`. (Thus you can make lists of primitive types, there.) Just some -almost- useless trivia. – Felype Mar 11 '15 at 15:34