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,948 questions

51,890 answers

573 users

How to split an array and add the first part to end in Java

1 Answer

0 votes
public class MyClass {
    public void print(int []arr)  {
        int size = arr.length;
         
        for (int i = 0; i < size; i++) {
          System.out.print(arr[i] + " ");
        }
        System.out.print("\n");
    }
    void reverse(int []arr, int start, int end) {
        int temp = 0;
     
        for (int i = start, j = end; i <= end && j > i; i++, j--)  {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    void split(int []arr, int split_point) {
        int size = arr.length;
 
        if (size <= 1 && split_point < 1 && split_point >= size) {
            return;
        }
 
        // reverse first part
        reverse(arr, 0, split_point - 1);
 
        // reverse second part
        reverse(arr, split_point, size - 1);
 
        // reverse all array 
        reverse(arr, 0, size - 1);
    }
    public static void main(String args[]) {
        MyClass obj = new MyClass();
 
        int []arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
     
        int split_point = 3;
 
        obj.split(arr, split_point); 
         
        obj.print(arr);
    }
}
 
 
 
/*
run:
 
4 5 6 7 8 9 0 1 2 3  
 
*/

 



answered Nov 29, 2021 by avibootz
edited Nov 29, 2021 by avibootz

Related questions

1 answer 225 views
1 answer 213 views
1 answer 179 views
1 answer 200 views
1 answer 184 views
...