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!

3
  • 5
    short answer is no; long answer is noooooooooo Commented Jun 20, 2016 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. Commented Jun 20, 2016 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. Commented Jun 20, 2016 at 19:01

4 Answers 4

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]

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

This doesn't do what the OP specifically asked about.
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.

2 Comments

"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.
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.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.