简体   繁体   English

如何通过Java中的stride切片int数组?

[英]How can I slice an int array by stride in Java?

I am learning Java. 我正在学习Java。

In Python, I can: 在Python中,我可以:

my_list = [1,2,3,4]
my_new_list = my_list [::-1]
print my_new_list                # [4,3,2,1]

Is there any method I can use in Java to do that on int array? 我可以在Java中使用任何方法在int数组上执行此操作吗?

Edit1: Sorry for the bad example. 编辑1:抱歉这个坏例子。 How about my_list [::-2]? my_list [:: - 2]怎么样?

Edit2: What I mean my_list [::-2] is like Edit2:我的意思是my_list [:: - 2]就像

my_list = [1,2,3,4]
my_new_list = my_list [::-2]
print my_new_list                # [4,2]

Edit3: Slicing by stride in Python is to "check" the item in a list every step (stride).If the step is negative, Python would check the item from the end of the list to the beginning of the list. 编辑3:在Python中按步幅切片是在每一步(步幅)中“检查”列表中的项目。如果步骤为否定,Python将检查从列表末尾到列表开头的项目。 For example: 例如:

my_list = [1,2,3,4,5,6,7,8,9]
my_list1 = my_list[::2]       # my_list1 becomes [1,3,5,7,9]
my_list2 = my_list[::3]       # my_list2 becomes [1,4,7]
my_list3 = my_list[::4]       # my_list3 becomes [1,5,9]
my_list4 = my_list[::-3]      # my_list4 becomes [9,6,3]

You can convert the array to list then use Collections.reverse() on that list. 您可以将数组转换为列表,然后在该列表上使用Collections.reverse()

Integer a[]={1,2,3,4};
List <Integer> list = Arrays.asList(a);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);

DEMO DEMO

You can do it in following way, with the help of Collections#reverse and for using that method we first have to convert array to list and after reversing the list we will again convert it to array. 您可以通过以下方式执行此操作,在Collections#reverse的帮助下,对于使用该方法,我们首先必须将数组转换为列表,然后在反转列表后,我们将再次将其转换为数组。 You should better use List instead of array if possible . 应该更好地利用List如果可能的话,而不是阵列。

public class Test {

    public static void main(String[] args) {
        Integer[] arr = new Integer[] { 1, 2, 3, 4 };
        List<Integer> list = Arrays.asList(arr);
        Collections.reverse(list);
        arr = list.toArray(new Integer[list.size()]);
        System.out.println(Arrays.toString(arr));
    }

}

OUTPUT OUTPUT

[4, 3, 2, 1]

Java does not have something builtin what you want, but you can built your own logic for it, something like this, just put the value in val , for example my_list[::2] 2 in this case val will be 2 Java没有内置你想要的东西,但你可以为它构建自己的逻辑,就像这样,只需将值放在val ,例如my_list [:: 2] 2在这种情况下val将为2

public static void main (String[] args) throws java.lang.Exception
{
int ml[] = {1,2,3,4,5,6,7,8,9};
int val = -2;
ArrayList al = new ArrayList();
if(val>0){
for(int i = 0 ; i<ml.length;i+=val){
    al.add(ml[i]);
}
}
else if(val<0){
for(int i = ml.length-1 ;  i>=0; i+=val){
    al.add(ml[i]);
}   

}


for(int i = 0 ; i<al.size();i++){
    System.out.println(al.get(i));
}

}

You can write it like this in Java 8 你可以在Java 8中这样写它

int a = { 1, 2, 3, 4 };
int step = -2;

int b = IntStream.range(0, a.length)
            .filter(i -> i%step == 0)
            .map(i -> a[(step < 0) ? a.length-i-1 : i])
            .toArray();

It should work with all example arrays and steps you provided. 它应该适用于您提供的所有示例数组和步骤。

Apache Commons Lang有一个实用方法:

ArrayUtils.reverse(int[] array)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM