Andrew
Andrew

Reputation: 11

Reversing Arrays

I'm just starting out with arrays and was wondering what I would have to add to this code to get the arrays to print out in reverse order.

import java.util.Scanner;
public class Program10 {

static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {

    int[] myArr = {5, 3, 2, 6, 14, 8, 9, 14};
    int[] myArr2 = {6, 7, 8, 9, 10};

    System.out.println("First array before being arranged");
    printArray(myArr);
    rearrangeArray(myArr);
    System.out.println("First array after being arranged");
    printArray(myArr);

    System.out.println("Second array before being arranged");
    printArray(myArr2);
    rearrangeArray(myArr2);
    System.out.println("Second array after being arranged");
    printArray(myArr2);                     

}

public static void printArray(int[] a){

}

public static void rearrangeArray(int[] b){

}
}

Upvotes: 0

Views: 84

Answers (5)

Ruelos Joel
Ruelos Joel

Reputation: 2399

How about converting your array into an ArrayList then pass it to Collections.reverse(array); to simply reverse your array.

Collections.reverse(new ArrayList(Arrays.asList(myArr)));

Upvotes: 0

Anh Phạm
Anh Phạm

Reputation: 71

public static void printArray(int[] a){
    for (int i = 0; i < a.length; i++){
        System.out.print(a[i] + " ");
    }
}

public static void rearrangeArray(int[] b){
    int start = 0;
    int end = b.length - 1;
    while (start < end){
        b[start] = b[start] + b[end];
        b[end] = b[start] - b[end];
        b[start] = b[start] - b[end];
        start++;
        end--;
    }
}

I think you are getting familiar with Java! :)

Upvotes: -1

Makoto
Makoto

Reputation: 106508

For posterity's sake, here's an approach in Java 8 that uses the Stream API to do this. If you're just going to use simple and small arrays, this is overkill, but is handy to know for something that may come up in the future.

int[] myArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Arrays.stream(myArr)
      .boxed() // required for the collect operation
      .collect(Collectors.toCollection(LinkedList::new))
      .descendingIterator() // is only present with a LinkedList
      .forEachRemaining(System.out::println);

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26077

1.) Print array by simple iterating over it.

2.) No need to Rearrange array, just print in reverse order.

static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {

    int[] myArr = { 5, 3, 2, 6, 14, 8, 9, 14 };
    int[] myArr2 = { 6, 7, 8, 9, 10 };

    System.out.println("First array before being arranged");
    printArray(myArr);
    rearrangeArray(myArr);
    System.out.println("First array after being arranged");
    printArray(myArr);

    System.out.println("Second array before being arranged");
    printArray(myArr2);
    rearrangeArray(myArr2);
    System.out.println("Second array after being arranged");
    printArray(myArr2);

}

public static void printArray(int[] a) {
    for (int element : a) {
        System.out.print(element);
    }
}

public static void rearrangeArray(int[] b) {
    for(int i = b.length-1; i>=0; i--){
          System.out.print(b[i]);
       }
}

output

First array before being arranged
5326148914First array after being arranged
1498146235Second array before being arranged
678910Second array after being arranged
109876

Upvotes: 0

Steve Knabe
Steve Knabe

Reputation: 120

public void printRev(int[] x){
   for(int i = x.length-1; i>=0; i--){
      System.out.print(x[i] + " ");
   }
}

In this example all you have to do is go from back to front.

Upvotes: 3

Related Questions