Giter Club home page Giter Club logo

Comments (5)

arpitverma123 avatar arpitverma123 commented on July 3, 2024

that's right

from java-se.

kuleen179 avatar kuleen179 commented on July 3, 2024

In for loop of reversing array there should be i-- not i++.

Try this..

reverseofArray.txt

from java-se.

kumarikajal05 avatar kumarikajal05 commented on July 3, 2024

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.

pankaj-254 avatar pankaj-254 commented on July 3, 2024

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.

Afzal14786 avatar Afzal14786 commented on July 3, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.