Return to Snippet

Revision: 69714
at August 21, 2015 10:29 by indecs


Initial Code
import java.util.Arrays;
public class Practive{
	public static void main (String[] args)
	{
		int [] array = new int[] {23,45,34,54,67,78};
		System.out.println("Original array:");
		System.out.println(Arrays.toString(array));
		//reverse method 1:
		int i =0;
		while ( i< (array.length)/2){		
			for (int j = array.length-1; j>i;j--){
				int tempf = array[i];
				int tempb = array[j];
				array[i] = tempb;
				array[j]= tempf;
				i++;
				System.out.println("Reverse array");
				System.out.println(Arrays.toString(array));	
			}
			
			
		}
		
		
		
	}
	
}


/*
	//reverse method 2:
import java.util.Arrays;
public class Practive{
public static void main (String[] args)
{
	int [] array = new int[] {23,45,34,54,67,78};
	System.out.println("Original array:");
	System.out.println(Arrays.toString(array));
	//reverse
	for (int i =0; i< (array.length)/2; i++){		
		for (int j = array.length-1; j>i;j--){
			int tempf = array[i];
			int tempb = array[j];
			array[i] = tempb;
			array[j]= tempf;
			i++;
			System.out.println("Reverse array");
			System.out.println(Arrays.toString(array));	
		}
		
		
	}
	
}

}
		
*/


/* 
import java.util.Arrays;
public class Practive{
public static void main (String[] args)
{
	int [] array = new int[] {23,45,34,54,67,78};
	System.out.println("Original array:");
	System.out.println(Arrays.toString(array));
	//reverse method 3
	for (int i =0; i< (array.length)/2; i++){	
		int temp = array[i];
		array[i] = array[array.length -1 -i];
		array[array.length-1-i]= temp;
		System.out.println("Reverse array");
		System.out.println(Arrays.toString(array));	
		}
		
		
	}
	
}
*/

Initial URL

                                

Initial Description
3 ways to reverse array in Java

Initial Title
3 ways to reverse array in Java

Initial Tags

                                

Initial Language
Java