I have a bunch of integers, more than 20, some of them a sequential, like from 100 to 109, but others are not, is there a efficient way to group them into a array? I tried use ArrayListand then list.toArray, but then i need to use too many add method. is there a more efficient way? thanks
Add a comment
|
2 Answers
If I understand correctly you have some predefined values and you want to build an efficient array. Easiest way would be to declare it in the following way:
int[] array = {45, 47, 84, 29};
While will produced an int[] (inferred from left hand side)
2 Comments
flungo
better than doing 50 .adds if your looking for performance. and easier than doing
new int[50]; array[0] = 45; array[1] = 47 If there is a source for the data or a generator then it may be easier but you stated there was not and you clearly want to use arrays in the most efficient way so that's the solution I provided. Feel free to expand on what you want and I will try to improve my answer.padawan
Where are those 50 numbers and in what format are they written?
If I understand you correctly, you could use Arrays.asList()
List<Integer> al = Arrays.asList(1,2,3,4,5,6,7,8); // keep adding ints.