Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,974 questions

51,918 answers

573 users

How to reverse an int array in Java

2 Answers

0 votes
import java.util.Arrays;

public class MyClass {
    static void reverseIntArray(int array[]) {
        int left = 0, right = array.length - 1;
        
        while (left < right) {
            int temp = array[left];
            array[left] = array[right];
            array[right] = temp;

            left++;
            right--;
        }
    }
    
    public static void main(String args[]) {
        int[] array = {1, 2, 3, 4, 5, 6};

        reverseIntArray(array);
        
        System.out.println(Arrays.toString(array));
    }
}
    
   
   
    
/*
run:
   
[6, 5, 4, 3, 2, 1]
 
*/

 



answered Nov 4, 2023 by avibootz
0 votes
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class MyClass {

    public static void main(String args[]) {
        int[] array = {1, 2, 3, 4, 5, 6};

        List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());

        Collections.reverse(list);

        array = list.stream().mapToInt(i -> i).toArray();
        
        System.out.println(Arrays.toString(array)); 
    }
}
    
   
   
    
/*
run:
   
[6, 5, 4, 3, 2, 1]
 
*/

 



answered Nov 4, 2023 by avibootz

Related questions

1 answer 123 views
123 views asked Oct 13, 2019 by avibootz
2 answers 228 views
1 answer 169 views
1 answer 120 views
1 answer 99 views
1 answer 106 views
1 answer 119 views
...