-
Reversing an Array
Reversing an Array
Write a program that prompts the user for an integer, then asks the user to enter that many values. Store these values in an
array and print the array. Then reverse the array elements so that the first element becomes the last element, the second
element becomes the second to last element, and so on, with the old last element now first. Do not just reverse the order in
which they are printed; actually change the way they are stored in the array. Do not create a second array; just rearrange the
elements within the array you have. (Hint: Swap elements that need to change places.) When the elements have been
reversed, print the array again.
My program compiles and run but, I need someone to check this to see if it is right. Make sure that I have everything that it needs. Thanks
//****************************************************************
// ReverseArray.java
//
//
// Program that prompts the user for an integer, enter many value
// prints out the array, reverses the array then outputs result
//****************************************************************
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
int size, temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of your array: ");
size = scan.nextInt();
int[] myArray = new int[size];
// input array of ints
for(int i = 0; i< myArray.length; i++){
System.out.print("Enter number " + i+": ");
myArray[i] = scan.nextInt();
}
// print original array
printArray(myArray);
//reverse elements in array using swap technique
for(int j = 0; j<(myArray.length/2); j++){
temp = myArray[j];
myArray[j] = myArray[myArray.length-1-j];
myArray[myArray.length-1-j] = temp;
}
// print reversed array
printArray(myArray);
}
// printing an int array
public static void printArray(int[] a){
for(int i =0; i<a.length;i++)
System.out.print(a[i]+"\t");
System.out.println();
}
}
-
You can't test it yourself? Just run the program, input number 1-10 in order. Should be fairly simple to see if it works or not.
-
It compiles and run, but I just wanted to make sure that I have everything that the program was asking for that's all.
-
Can you think of a way to do your reversing using a while loop?
-
As programmer, first you need is confidence. If you are not confident enough to say that your code will work fine, then nobody can help you..
Try doing a lot of programs like this..that will give enough confidence..
By the way, why can't you try reversing without using a variable temp?
-
You can use a stack if you just only need to reverse elements in this array
-
j2se library has already done for you.
public class ReverseArray
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
size = scanner.nextInt();
List result = new LinkedList();
for(int i = 0; i< myArray.length; i++)
{
result.add(Integer.valueOf(scan.nextInt()));
}
Collections.reverse(result);
printIntergers(result.toArray());
}
}
simple!
-
Reverse and add Array from user entered input
import java.util.List;
import java.util.ArrayList;
import java.util.*;
public class ReverseArray{
public static void main(String[] args){
List arraylist = new ArrayList();
Scanner in = new Scanner(System.in);
System.out.print("How many elements does the array have? ");
int b = in.nextInt();
System.out.println("Enter array elements: ");
for (int i = 0; i < b; i++)
{
int a = in.nextInt();
arraylist.add(a);
}
Object[] array = arraylist.toArray();
System.out.print("The objects in array: ");
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i].toString() + " ");
}
System.out.println();
System.out.print("The reverse array is: ");
reverse(array);
System.out.println();
System.out.print("The sum of array elements is: ");
add(array);
}
// Reverse the elements of the array
public static void reverse(Object[] backward)
{
for(int i = (backward.length -1); i >=0;i--)
{
System.out.print(backward[i] + " ");
}
}
// Method that adds elements of the array
public static void add(Object[] add)
{
int sum = 0;
for(int i = 0; i < add.length ; i++)
{
String a = add[i].toString();
sum = sum + (Integer.parseInt(a));
}
System.out.print(sum);
}
}
Similar Threads
-
Replies: 4
Last Post: 11-08-2006, 03:19 PM
-
By angelito in forum VB Classic
Replies: 1
Last Post: 11-21-2005, 06:16 AM
-
Replies: 6
Last Post: 11-01-2005, 09:05 AM
-
By kanakatam in forum Java
Replies: 2
Last Post: 04-15-2005, 09:06 PM
-
By Mark Alexander Bertenshaw in forum VB Classic
Replies: 0
Last Post: 06-12-2000, 08:15 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks