4

I am learning Java.

In Python, I can:

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?

Edit1: Sorry for the bad example. How about my_list [::-2]?

Edit2: What I mean my_list [::-2] is like

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. 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]
Jay Wang
  • 2,650
  • 4
  • 25
  • 51

5 Answers5

4

You can convert the array to list then use Collections.reverse() on that list.

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

DEMO

singhakash
  • 7,891
  • 6
  • 31
  • 65
1

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. You should better use List instead of array if possible.

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

[4, 3, 2, 1]
akash
  • 22,664
  • 11
  • 59
  • 87
1

You can write it like this in 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.

dejvuth
  • 6,986
  • 3
  • 33
  • 36
0

Apache Commons Lang has an utility method for that:

ArrayUtils.reverse(int[] array)
Jan
  • 13,738
  • 3
  • 30
  • 55
0

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

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));
}

}
Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
  • @JayWong this code is a mess - I would not use it. You would be better off with a smart `Iterator` design to create a "view" of the underlying data. – Boris the Spider Dec 29 '15 at 13:29
  • I would appreciate if you provide him a better solution rather by telling him how to do, he is newbie in java. – Sindhoo Oad Dec 30 '15 at 04:05