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; }
To sort Short array, use the Arrays. sort() method. Let us first declare and initialize an unsorted Short array. short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 };
int[] myArray; To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15};
Given a number N, the task is to create an array arr[] of size N, where the value of the element at every index i is filled according to the following rules: arr[i] = ((i – 1) – k), where k is the index of arr[i – 1] that has appeared second most recently.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With