The Artima Developer Community
Sponsored Link

Java Answers Forum
reverse array in java

3 replies on 1 page. Most recent reply: May 4, 2009 11:38 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Aaron Glazier

Posts: 1
Nickname: buckman412
Registered: Apr, 2009

reverse array in java Posted: Apr 4, 2009 2:42 PM
Reply to this message Reply
Advertisement
i've been trying to get this array to reverse then print the reverse. well i do believe i have my logic right and everytime i compile it works fine, then when i run it gives me an error, this code is in java format, if anyone could help me or tell me what i'm doing wrong that would be great.

my code is:
public class reverseArrayTwo
{

public static void main(String[] args)
{
String[] forwardArray ={"one", "two", "three", "four", "five", "six"};
String[] backwardArray =new String [6];
int forwardIndex = 0;
for (int i = 0; i < forwardArray.length; i++)
{
backwardArray[forwardArray.length - i] = forwardArray;
}

System.out.print(forwardArray);
System.out.print(backwardArray);

}
}


bnafsaj alsaadi

Posts: 3
Nickname: bnafsaj
Registered: May, 2009

Re: reverse array in java Posted: May 1, 2009 7:08 AM
Reply to this message Reply
i'm not that much good in programming
but i think u need to store the elements in temp variable and reverse
also, i think u need to print the array befor reversing then do the reversem and finaly print the reverse

bnafsaj alsaadi

Posts: 3
Nickname: bnafsaj
Registered: May, 2009

Re: reverse array in java Posted: May 1, 2009 7:12 AM
Reply to this message Reply
public class reverseArrayTwo
{

public static void main(String[] args)
{
String[] forwardArray ={"one", "two", "three", "four", "five", "six"};
System.out.print(forwardArray);

String[] backwardArray =new String [6];
int forwardIndex = 0;
for (int i = 0; i < forwardArray.length; i++)
{
String temp = forwaredArray;
backwardArray[forwardArray.length - i] = temp;
}

System.out.print(backwardArray);

}
}

i do not if this will work, but this what i know

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: reverse array in java Posted: May 4, 2009 11:38 PM
Reply to this message Reply
my code is:
public class reverseArrayTwo
{
 
 	
 	String[] backwardArray =new String [6];
 				int forwardIndex = 0;
 	for (int i = 0; i < forwardArray.length; i++)
 		{
//  The first time in the loop this will give an Array Out
of bounds.
 
i.e. first time i = 0.  Length - 0 is index [6] remember Arrays work from Index 0 meaning an Array of size 6 will have indeces up to 5.  i.e. ArrayOutOfBoundsException.
 
Maybe rework your logic to factor this in.
 
 backwardArray[forwardArray.length - i] =
 ] = forwardArray[i];
 			}
 		
 	System.out.print(forwardArray);
 	System.out.print(backwardArray);
 		 
 	}
> }

Flat View: This topic has 3 replies on 1 page
Topic: Question in Java Programming(GUI) Previous Topic   Next Topic Topic: ESB - first steps

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use