Is there anything as quick as this in java? ( quick in coding)
int [] a = {1..99};
or I have to go for this:
int [] a=new int[100];
for (int i=0;i <100;++i){
a[i]=i;
}
Is there anything as quick as this in java? ( quick in coding)
int [] a = {1..99};
or I have to go for this:
int [] a=new int[100];
for (int i=0;i <100;++i){
a[i]=i;
}
Since Java 8 this is possible:
int[] a = IntStream.range(1, 100).toArray();
(And shorter than the other java 8 answer .).
Another alternative if you use Java 8:
int[] array = new int[100];
Arrays.setAll(array, i -> i + 1);
The lambda expression accepts the index of the cell, and returns a value to put in that cell. In this case, cells 0 - 99 are assigned the values 1-100.
Java 8 allows to do that in one line with IntStream
object and lambda expression:
int n = 10;
int[] values = new int[n];
IntStream.range(1,n+1).forEach(val -> values[val-1] = val);
Out of curiosity, I have tested the performance of two versions of that method - one with a loop and the other one using guava:
public int[] loop() {
int[] a = new int[100];
for (int i = 0; i < 100; ++i) {
a[i] = i;
}
return a;
}
public int[] guava() {
Set<Integer> set = ContiguousSet.create(Range.closed(0, 99), DiscreteDomains.integers());
int[] a = Ints.toArray(set);
return a;
}
Here are the results:
Benchmark Mean Mean error Var Units
loop 79.913 5.671 30.447 nsec/op
guava 814.753 46.359 2034.726 nsec/op
So the guava()
method runs in 814 ns +/- 46ns vs. 80 ns +/- 5ns for the loop()
method. So loop()
is about 10x faster. If you call that method a few times, the 800 nanoseconds don't matter, if you call it very often, writing the loop is probably better.
I think that your code is the shortest and the simplest way. You might dont need to load extra libraries to get more "compact" code lines. The for loops are very simple (a truly O(n)) and legible, live and love them.
depending on the size you will have to loop, if its a small one you can do the following...
int[] intArray = new int[] {4,5,6,7,8};
im guessing for your size you dont want to have to type it all out so makes sense to create a loop and set it that way
If people do want to use the for-loop method, then you could shorten it to (side note):
int [] a = new int[100];
for (int i = 0; i < 100; a[i] = i++);
You can use Guava library, for something like this:
public class Test {
public static void main(String[] args) {
//one liner
int[] array = toArray(newLinkedList(concat(range(1, 10), range(500, 1000))));
//more readable
Iterable<Integer> values = concat(range(1, 10), range(500, 1000));
List<Integer> list = newLinkedList(values);
int[] array = toArray(list);
}
public static List<Integer> range(int min, int max) {
List<Integer> list = newLinkedList();
for (int i = min; i <= max; i++) {
list.add(i);
}
return list;
}
}
Updated: full example take from this post Fill arrays with ranges of numbers
In case someone is not able to use java 8+, here is a solution with Google Guava:
Long[] value = ContiguousSet.create(Range.closed(0L, 23L), DiscreteDomain.longs())
.toArray(new Long[0]);
You must use loop to initialize such a long array. There is not a shortcut method in Java as you expected.