How to do Python-style array slicing in Java?

74 views Asked by At

I am a Python developer and I am completely new to Java. Is there a way in Java to do something like this?

array[start:end] = [True] * (end -start)

and something like this?

array[start:end:2] = [True] * ((end-start)/2) 

I found something like this:

Arrays.copyOfRange(oldArray, startIndex, endIndex);

but I do not know how to use it in my case.

3

There are 3 answers

0
Chris Neagu On
  1. Not, only like this
    for(int i = start; i < end; i++) {
        array[i] = true;
    }
  1. Same like 1.
    for(int i = start; i < end; i+=2) {
       array[i] = true;
    }
  1. You don't modify the array if u don't assign it.
    boolean[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);
0
Mr. Polywhirl On

Your Python should be:

start, end = 2, 6

arr = [False] * 10
arr[start:end] = [True] * (end - start)
print(arr)
# [False, False, True, True, True, True, False, False, False, False]

arr = [False] * 10
arr[start:end:2] = [True] * ((end - start) // 2)
print(arr)
# [False, False, True, False, True, False, False, False, False, False]

An idiomatic Java approach would be to create a method that assigns the true value in a loop:

import java.util.Arrays;

public class ArrayAssignment {
    public static void main(String[] args) {
        int start = 2, end = 6;

        boolean[] arr = new boolean[10];
        assignValue(arr, true, start, end);
        System.out.println(Arrays.toString(arr));
        // [false, false, true, true, true, true, false, false, false, false]

        boolean[] arr2 = new boolean[10];
        assignValue(arr2, true, start, end, 2);
        System.out.println(Arrays.toString(arr2));
        // [false, false, true, false, true, false, false, false, false, false]
    }

    public static boolean[] assignValue(boolean[] arr, boolean value, int start,
            int end, int step) {
        for (int i = start; i < end && i < arr.length; i += step) {
            arr[i] = value;
        }
        return arr;
    }

    public static boolean[] assignValue(boolean[] arr, boolean value, int start,
            int end) {
        return assignValue(arr, value, start, end, 1);
    }
}
0
Mark Rotteveel On

Specifically for arrays and the first example given, you could look at methods like Arrays.fill(boolean[], int, int, boolean):

Arrays.fill(array, start, end, true)

However, in general, you can't do slicing with arrays in Java, and it would be more commonly solved with - for example - sub-lists, using List.subList(int, int), and modifying the sub-list (e.g. in a loop). Changes to a sub-list are reflected in the original list. This does require that the operation is done on a (fully) modifiable list (operations which only replace elements may get away with a modifiable fixed length list like returned by Arrays.asList, but if you also want to add or remove items, you'll need to use a fully modifiable list like an ArrayList.

However, there is no ready equivalent for slicing with a step, so that can only be solved by looping as shown by the other answers.