-4

My teacher gave us this whole code and wants us to fill it in but she never really taught at all on how to do arrays in general so I'm confused on the fact on how I should fill it in and the whole understanding of arrays.

public class Reverser
{

    public static void main ( String[ ] args )  {
        int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};

        // reverse the data
        for ( int j=0; j < be careful here; j++) {

        }

        // Print out the new data
        for ( int j=0; j < data.length; j++) {

        }

    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
llZwei
  • 1
  • Unclear if you are to actually reverse the array, or just print in reverse.... – OneCricketeer Oct 25 '16 at 03:06
  • 2
    I understand your frustration but any answer here would be a repeat of one of the many tutorials on java arrays that already exist. Try looking up a tutorial in your search engine of choice. [Here is one](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) – qfwfq Oct 25 '16 at 03:08
  • This is a very basic exercise in programming. Many online tutorials should cover this. Some quick googling turned up https://www.tutorialspoint.com/java/java_arrays.htm and here is a video about this exact problem is one using C but Java in this case is fairly similar: https://www.youtube.com/watch?v=C73wXCnZutQ. – Eli Algranti Oct 25 '16 at 03:13

3 Answers3

2
public class Reverser
{

    public static void main ( String[ ] args ) 
    {
        int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};

        // reverse the data
        for ( int j=0; j < data.length / 2; j++)
        {
            int tmp = data[j];
            data[j] = data[data.length - j - 1];
            data[data.length - j - 1] = tmp;
        }

        // Print out the new data
        for ( int j=0; j < data.length; j++)
        {
            System.out.println(data[j]);
        }

    }
}

ADDED EXPLANATION :

Reversing is just exchange some value's place in the list;

For example,

The first value need to exchange with the last one. And the second value need to exchange with the second last one.

Now we can find out the rule that

the element

data[j]

will need to exchange with

data[size - j]

where j starts from 0

And it's easy to find out that when j comes to the half of the array size, we have actually done all exchanges need to be done.

T.Tony
  • 495
  • 3
  • 15
0
        int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
        for (int i = 0; i < (data.length / 2 + 1); i++ ) {
            int temp = data[i];
            data[i] = data[data.length - i - 1];
            data[data.length - i - 1] = temp;
        }
Dale
  • 191
  • 1
  • 1
  • 12
0
public class ReverseArray {

    public static void main(String[] args) {
        ReverseArray r = new ReverseArray();
        r.printReverseArray();
    }

    int[] array = new int[]{2, 4, 5, 6, 7, 8, 9, 10, 11};

    void printReverseArray() {
        for (int i = array.length-1; i > -1; i--) {
            System.out.println(array[i]);
        }
    }
}
KVISH
  • 12,923
  • 17
  • 86
  • 162