reverse array in java
int length = array.length;
for(int i=0;i<length/2;i++) {
int swap = array[i];
array[i] = array[length-i-1];
array[length-i-1] = swap;
}
or
Collections.reverse(Arrays.asList(array));
reverse array in java
int length = array.length;
for(int i=0;i<length/2;i++) {
int swap = array[i];
array[i] = array[length-i-1];
array[length-i-1] = swap;
}
or
Collections.reverse(Arrays.asList(array));
Reversing an array
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,temp,i,j;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}
cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us