I am trying to write a code to print the elements of an array called top10 backwards. How would this be? Anything would be great as I have no clue how to begin.
-
you start from last 10 by setting initial index = 9, for (var i = array.length-1; i >= 0 ; i--) console.log(array[i]) – Ajanth Jun 18 '18 at 23:48
-
You start at `0` and loop to `9` and request the indices in reverse order (ie `top10[top10.length - index - 1]` or you could loop from `9` down to `0` printing each element. Take a look at [The for Statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) for more details – MadProgrammer Jun 18 '18 at 23:54
-
Possible duplicate of [Looping through the elements in an array backwards](https://stackoverflow.com/questions/9379489/looping-through-the-elements-in-an-array-backwards) – 23k Nov 19 '18 at 05:07
3 Answers
It would look something like this:
int topTenBackwards[] = new int[] {1,2,3,4,5,6,7,8,9,10};
for(int i = topTenBackwards.length - 1; i >= 0 ; i--) {
System.out.println(topTenBackwards[i]);
}

- 79
- 1
- 8
To reverse an int array, you swap items up until you reach the midpoint, like this:
for(int i = 0; i < validData.length / 2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
This is taken from another question, here:
How do I reverse an int array in Java?
I think its what you need. You just reverse it, and then iterate and print the first 10, with a function like:
int[] myarray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
for (int element: myarray) {
System.out.println(element);
}
So, endgame should look something like:
int[][] myarray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
for(int i = 0; i < myarray.length / 2; i++)
{
int[] temp = myarray[i];
myarray[i] = myarray[myarray.length - i - 1];
myarray[myarray.length - i - 1] = temp;
}
// array reversed
int x = 0;
for (int[] element: myarray) {
System.out.println(element);
x++;
if (x == 10) {
break;
}
}
// array printed
Using the int[] element: myarray
gives me all the items in the array, and the break
stops the printing loop when you reach the 10th, if you don't, it still stops when you run out of items (like with just 9)
If your array is multi-dimensional you can just nest the for-loops to iterate the inner levels, or use the Arrays.toString(myarray)
function from java.util.Arrays;
Printing arrays taken from: Java Program to Print an Array - Programiz
Hope it helps. (If I made a mistake, I'm happy to be corrected)

- 5
- 5
-
Thank you so much for you help. That helps a lot, my finals in 24 hours so thank you once again. – lostprogrammer Jun 19 '18 at 16:01
I am writing here the code for Printing elements of an array called top10 backward. There are a lot of resource through you find it in a different language like PHP, C++, Python, and all.
public class GFG {
/* Function to reverse arr[] from
start to end*/
static void rvereseArray(int arr[],
int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
/* Utility that prints out an
array on a line */
static void printArray(int arr[],
int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver code
public static void main(String args[]) {
int arr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
rvereseArray(arr, 0, 5);
System.out.print("Reversed array is \n");
printArray(arr, 6);
}
}
Here I am also suggesting you learn from geeks for geeks, and other programming blogs.

- 21
- 5