52

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;
}
C graphics
  • 7,308
  • 19
  • 83
  • 134

10 Answers10

120

Since Java 8 this is possible:

int[] a = IntStream.range(1, 100).toArray();

(And shorter than the other java 8 answer .).

Bachi
  • 6,408
  • 4
  • 32
  • 30
  • Arrays.setAll() is a much better way. –  Jul 28 '17 at 20:22
  • 1
    @Arkadiy might be a matter of taste but IMHO using `IntStream#toArray` is much better to read than `Arrays#setAll`. – Marteng Mar 14 '18 at 15:33
  • Is there any way to create an array of User-defined classes? in same fashion we have created `int[] a` – Pravin Apr 09 '19 at 01:47
  • 3
    The `.rangeClosed` method is inclusive, filling the array to 100 elements `int[] a = IntStream.rangeClosed(1, 100).toArray();` https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html – phillipsK Apr 10 '19 at 11:35
  • Minimum API level is 24? – Abu Nayem Nov 23 '20 at 16:09
23

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.

Mark Peschel
  • 115
  • 3
  • 10
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
19

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);
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
chao
  • 1,893
  • 2
  • 23
  • 27
16

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
assylias
  • 321,522
  • 82
  • 660
  • 783
2

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.

1

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

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Andy B
  • 328
  • 3
  • 9
1

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++);
0

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

evgenyl
  • 7,837
  • 2
  • 27
  • 32
  • the `range` method is not part of guava and has been written using a loop in that answer. It does not help here. – assylias Apr 15 '13 at 17:38
  • Probably you are right, i remember that ot was something like this, but took example from linked post as is – evgenyl Apr 15 '13 at 17:47
0

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]);
-1

You must use loop to initialize such a long array. There is not a shortcut method in Java as you expected.

Nobal
  • 77
  • 1