my code should print numbers in reverse array
its not have error
but the lest for statement its not print array in reverse order

import java .util.Scanner;
public class R {

    public static void main (String [] args){
    	Scanner input=new Scanner(System.in);
    	int size;
    	System.out.println("Size ");
    	size=input.nextInt();
    	int [] array=new int [size];
    	System.out.println("enter element ");
    	for(int i=0;i<array.length;i++)
    		array[i]=input.nextInt();
    			System.out.println(" element of array is ");
    			for(int i=0;i<array.length;i++)
    		System.out.println(" "+array[i]);
int arrayb[]=new int[size];
 for(int i=0;i<array.length;i++)
			 {            arrayb[i] = array[i];        }
			 	System.out.println(" element of array befor reserved ");
			 	for(int i=0;i<arrayb.length;i++)
    		System.out.println(" "+arrayb[i]);
System.out.println(" element of array after reserved ");
for(int i=arrayb.length-1;i<=0;i--)
System.out.println(" "+arrayb[i]);
}}

Recommended Answers

All 7 Replies

the lest for statement its not print array in reverse order

Please post the program's output that shows the problem.

You need to edit your code and properly align the statements.
The messy way you have posted the code makes it very hard to read.

You should align the } vertically beneath the statement with the starting {
Do NOT put two }}s on the same line.
Do not put a statement on the same line following a {

here i think is problem

for(int i=arrayb.length-1;i<=0;i--)
System.out.println(" "+arrayb[i]);

output

--------------------Configuration: <Default>--------------------
Size 
2
enter element 
1
2
 element of array is 
 1
 2
 element of array befor reserved 
 1
 2
 element of array after reserved 

Process completed.

It looks like NOTHING is printed for the reversed array. Is that right?
Look at the for loop controls to be sure that you are using them correctly.

Read this:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

especially the part about the termination of the loop

i try to find error
i think its correct in my perspective

Copy the for loop's termination expression here and explain how you think it is working.

for(int i=arrayb.length-1;i<=0;i--)

loor start from the last elemnt to first element in array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.