Comments (5)
that's right
from java-se.
In for loop of reversing array there should be i-- not i++.
Try this..
from java-se.
That's right, Here's what the code does:
The code declares a new integer array 'a' with some initial values {1, 2, 3, 4, 5, 6, 7, 8}.
It declares a new integer array 'b' with the same length as array 'a'.
It uses 'a' for loop to iterate through array 'a' from the end to the beginning. During each iteration, it assigns the current value of 'a[i]' to the corresponding position in array 'b', starting from the beginning.
Finally, it uses a for-each loop to print out each element in array 'b' in the reversed order.
So the output of the program would be:' 8 7 6 5 4 3 2 1'.
from java-se.
try this
public class MyClass {
public static void main(String args[]) {
int x[]={1, 2, 3, 4, 5, 6, 7, 8};
//not reverse this array x
//first create a array of same length as 'x'
int y[]=new int[x.length];
//now traverse all the elements of x and assign to y in reverse manner
for(int i=0;i<x.length;i++)
{
//here we are assigning element of y in reverse manner
y[i]=x[x.length-i-1];
}
for(int X:y)
{
System.out.print(X+",");
}
}
}
from java-se.
it is also possible to reverse the same array using constant space with O(n) time complexity .//
class Main {
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
**// it just return the array in reverse manner without any extra space or any temp array //**
for (int i = array.length-1; i>=0; i--) {
System.out.print(array[i] +" ");
}
}
}
from java-se.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java-se.