Reversing an Array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Reversing an Array

  1. #1
    Join Date
    Nov 2006
    Posts
    5

    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();
    }
    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    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.

  3. #3
    Join Date
    Nov 2006
    Posts
    5
    It compiles and run, but I just wanted to make sure that I have everything that the program was asking for that's all.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Can you think of a way to do your reversing using a while loop?

  5. #5
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    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?

  6. #6
    Join Date
    Oct 2006
    Posts
    2
    You can use a stack if you just only need to reverse elements in this array

  7. #7
    Join Date
    Dec 2006
    Posts
    3
    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!

  8. #8
    Join Date
    Feb 2009
    Posts
    1

    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

  1. Replies: 4
    Last Post: 11-08-2006, 03:19 PM
  2. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  4. Replies: 2
    Last Post: 04-15-2005, 09:06 PM
  5. SafeArrayCopy SLOWER than iterating string array!
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center