0

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

0

2 Answers 2

1

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)

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

2 Comments

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.
Where are those 50 numbers and in what format are they written?
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.