0

I have an array of n elements and these methods:

  • last() return the last int of the array
  • first() return the first int of the array
  • size() return the length of the array
  • replaceFirst(num) that add the int at the beginning and returns its position
  • remove(pos) that delete the int at the pos

I have to create a new method that gives me the array at the reverse order. I need to use those method. Now, I can't understand why my method doesn't work. so for (int i = 1; i

The remove will remove the element at the position i, and return the number that it is in that position, and then with replaceFirst will move the number (returned by remove) of the array.

I made a try with a simple array with {2,4,6,8,10,12} My output is: 12 12 12 8 6 10

so if I have an array with 1,2,3,4,5

  • for i = 1; I'm gonna have : 2,1,3,4,5
  • for i=2 >3,2,1,4,5
  • etc

But it doesn't seem to work.

  • 7
    Show us the code you have written so far. – tutak Apr 11 '14 at 08:08
  • 2
    Write code and if you have specific questions / problems ask about them, people here will not do your work. – LionC Apr 11 '14 at 08:09
  • To reverse an array begins with the last element of the array (you know the length) and down to the first element. (Key words: For, array.length) – Averroes Apr 11 '14 at 08:09
  • Bear in mind that an index (or iterator) into an array that is being modified can end up pointing at incorrect items. – Deanna Apr 11 '14 at 08:20
  • I dint ask to do my work, I also posted all my code. – exclusive-OR Apr 11 '14 at 08:49
  • @exclusive-OR Can I modify the methods you have posted? e.g first() doesn't return the first element of the array, it just returns 1. – tutak Apr 11 '14 at 09:30

10 Answers10

1

Well, I'll give you hints. There are multiple ways to reverse an array.

  • The simplest and the most obvious way would be to loop through the array in the reverse order and assign the values to another array in the right order.
  • The previous method would require you to use an extra array, and if you do not want to do that, you could have two indices in a for loop, one from the first and next from the last and start swapping the values at those indices.
  • Your method also works, but since you insert the values into the front of the array, its going to be a bit more complex.
  • There is also a Collections.reverse method in the Collections class to reverse arrays of objects. You can read about it in this post
Community
  • 1
  • 1
anirudh
  • 4,116
  • 2
  • 20
  • 35
0

Here is an code that was put up on Stackoverflow by @unholysampler. You might want to start there: Java array order reversing

public static void reverse(int[] a)
{
    int l = a.length;
    for (int j = 0; j < l / 2; j++)
    {
        int temp = a[j]
        a[j] = a[l - j - 1];
        a[l - j - 1] = temp;
    }
}
Community
  • 1
  • 1
Nathaniel Payne
  • 2,749
  • 1
  • 28
  • 32
0

Reversing an array is a relatively simple process. Let's start with thinking how you print an array normally.

int[] numbers = {1,2,3,4,5,6};

for(int x = 0; x < numbers.length; x++)
{
    System.out.println(numbers[x]);
}

What does this do? Well it increments x while it is less than numbers.length, so what is actually happening is..

First run : X = 0

System.out.println(numbers[x]);
// Which is equivalent to..
System.out.println(numbers[0]);
// Which resolves to..
System.out.println(1);

Second Run : X = 1

System.out.println(numbers[x]);
// Which is equivalent to..
System.out.println(numbers[1]);
// Which resolves to..
System.out.println(2);    

What you need to do is start with numbers.length - 1, and go back down to 0. To do this, you need to restructure your for loop, to match the following pseudocode..

for(x := numbers.length to 0) {
    print numbers[x]
}

Now you've worked out how to print, it's time to move onto reversing the array. Using your for loop, you can cycle through each value in the array from start to finish. You'll also be needing a new array.

int[] revNumbers = new int[numbers.length];

for(int x = numbers.length - 1 to 0) {
    revNumbers[(numbers.length - 1) - x] = numbers[x];
}
christopher
  • 26,815
  • 5
  • 55
  • 89
0
int[] reverse(int[] a) {
    int len = a.length;
    int[] result = new int[len];
    for (int i = len; i > 0 ; i--)
        result[len-i] = a[i-1];
    return result;
}
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0
for(int i = array.length; i >= 0; i--){
    System.out.printf("%d\n",array[i]);
}

Try this.

Aitor Gonzalez
  • 125
  • 1
  • 14
0

If it is a Java array and not a complex type, the easiest and safest way is to use a library, e.g. Apache commons: ArrayUtils.reverse(array);

Andy
  • 1,964
  • 1
  • 15
  • 29
0

In Java for a random Array:

public static void reverse(){

    int[] a = new int[4];

    a[0] = 3;
    a[1] = 2;
    a[2] = 5;
    a[3] = 1;

    LinkedList<Integer> b = new LinkedList<Integer>();

    for(int i = a.length-1; i >= 0; i--){

        b.add(a[i]); 

    }

    for(int i=0; i<b.size(); i++){

        a[i] = b.get(i);
        System.out.print(a[i] + ",");   
    }



}

Hope this helps.

luiscosta
  • 855
  • 1
  • 10
  • 16
0
int[] noArray = {1,2,3,4,5,6};
    int lenght = noArray.length - 1;

    for(int x = lenght ;  x >= 0; x--)
    {
        System.out.println(noArray[x]);
    }
}
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
0
    int[] numbers = {1,2,3,4,5};
    int[] ReverseNumbers = new int[numbers.Length];

    for(int a=0; a<numbers.Length; a++)
    {
        ReverseNumbers[a] = numbers.Length - a;
    }

    for(int a=0; a<ReverseNumbers.Length; a++)
    Console.Write(" " + ReverseNumbers[a]);
Bonga Mbombi
  • 175
  • 1
  • 11
0
int[] numbers = { 1, 2, 3, 4, 5, 6 };
reverse(numbers, 1); >2,1,3,4,5
reverse(numbers, 2); >3,2,1,4,5

public int[] reverse(int[] numbers, int value) {
int index = 0;
for (int i = 0; i < numbers.length; i++) {
  int j = numbers[i];
  if (j == value) {
    index = i;
    break;
  }
}
int i = 0;
int[] result = new int[numbers.length];
int forIndex = index + 1;
for (int x = index + 2; x > 0; x--) {
  result[i] = numbers[forIndex--];
  ++i;
}
for (int x = index + 2; x < numbers.length; x++) {
  result[i] = numbers[x];
  ++i;
}
return result;
}
Robin
  • 1
  • 1