Okay so I am using the JavaMail library and I am trying to fetch certain message numbers. I want to do it efficiently and not have to loop twice over something… Anyways my question to you is: How can I create an array that starts at index x and ends at index x - 11 without looping?
Okay so I am using the JavaMail library and I am trying to fetch
Share
If you want to create and populate an array, you have basically three options:
Write the values explicitly:
int[] nums = new int[] { 0, 1, 2, 3, 4, ... }Use some form of for-loop:
for (int i = 0; i < 10; i++) { nums[i] = i; }Create it recursively:
int[] nums = new int[12]; nums = populate(0, x, nums); private int[] populate(int index, int x, int[] nums) { if (nums.length >= index) { return nums; } else { nums[index] = x - index; // x-0 through x-11 return populate(index+1, x, nums); } }Vanilla Java, without extra libraries and whatnot, doesn’t support a map function which would allow you to specify a function that would somehow auto-generate your values.
Though, I really don’t understand why you don’t want to use a loop, especially for something trivial like this.