4

In python we are able to do the following:

 array = [0,1,2,3,4,5,6,7,8,9,10]
 new_array= array[::3]
 print(new_array)
>>>[0,3,6,9]

Is there an equivalent to this in Java? I have been looking for this type of array slicing, but I have had no luck. Any help would be great, Thanks!

garg10may
  • 5,794
  • 11
  • 50
  • 91
robman
  • 73
  • 1
  • 1
  • 4
  • 5
    short answer is **no**; long answer is **noooooooooo** –  Jun 20 '16 at 18:49
  • 1
    Is the duplicate really applicable? This question is asking about slicing at a set interval, whereas the linked question is more so tailored towards getting a general subset of the array (which is doable if it's in one continuous section), with mention of slicing only in the comments of one answer. Either way, I agree with the conclusion; there's no in-house way to do this, but you could easily write a helper function for it. – Ironcache Jun 20 '16 at 18:56
  • Ohh I thought he is trying to get the first few elements for the Array. sorry wrong answer for this question. Will remove the comment. Thankyou @Ironcache for pointing that out. – Arpit Ratan Jun 20 '16 at 19:01

4 Answers4

5

If you are using Java 8, then you can make use of streams and do the following:

int [] a = new int [] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// filter out all indices that evenly divide 3
int [] sliceArr = IntStream.range(0, a.length).filter(i -> i % 3 == 0)
    .map(i -> a[i]).toArray();

System.out.println(Arrays.toString(sliceArr));

Outputs: [0, 3, 6, 9]

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
3

There is a method in Arrays that might help.

 int[] newArr = Arrays.copyOfRange(arr, 5,10); 

It is obviously far less powerful the the python implementation.

gbtimmon
  • 4,238
  • 1
  • 21
  • 36
0

Java has no built-in mechanism for this. You could write a helper function:

public static int[] sliceArray(int[] arr, int spacing) {
    int curr = 0;
    int[] newArr = new int[((arr.length - 1) / spacing) + 1];
    for (int i = 0; i < newArr.length; ++i) {
        newArr[i] = arr[curr];
        curr += spacing;
    }
    return newArr;
}

Example

Note that Michael's answer is better (or at least less verbose) if you can utilize Java 8.

Ironcache
  • 1,719
  • 21
  • 33
  • "You can write a helper function" is not an answer for basic functionality that many languages do out of the box. Of course we can write it ourselves, we can always write it ourselves. – Shammoo Jul 11 '17 at 13:57
  • 2
    I don't get your point. **Java** (the language being discussed) **doesn't** do this out of the box (as I've stated in the answer); that's the problem, and no answer here will get around the lack of built-in grammar to handle this. – Ironcache Jul 11 '17 at 16:57
0

We can simply loop over array and collect values on every step

public static <T> List<T> sliceArray(List<T> arr,int start, int stop ,int step){
    System.out.println("slice with step initial = " + arr);
    List<T> res = new ArrayList<T>();
    for(int i=0;i<arr.size();i++){
        if(i%step==0) res.add(arr.get(i));
    }
    return res;
}