Hey need lots of help on arrays!

this is the exercise...

  1. Write a Java application that creates an array of int initialised with the following numbers:

    1 4 9 16 17 21 25 33 37 40

and reverses the contents of the array such that the value in the first element is the value that was at the last element and the value in the last element is the value that was in the first element. Below is an example of what the application should produce.

40 37 33 25 21 17 16 9 4 1



public class ArrayExercise1 {
    public static void main (String[] args)  {


    int[] values = new int[10];
    values[ 10 ] = 0;

thats my code at the moment, its not finished because im stuck and i really am not sure what to do! please help, i have to use int for it to work!

get back to me! thanks!

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Hey need lots of help on arrays!

this is the exercise...

1) Write a Java application that creates an array of int initialised with the following numbers:

1 4 9 16 17 21 25 33 37 40

and reverses the contents of the array such that the value in the first element is the value that was at the last element and the value in the last element is the value that was in the first element. Below is an example of what the application should produce.

40 37 33 25 21 17 16 9 4 1



public class ArrayExercise1 {
    public static void main (String[] args)  {


    int[] values = new int[10];
    values[ 10 ] = 0;

thats my code at the moment, its not finished because im stuck and i really am not sure what to do! please help, i have to use int for it to work!

get back to me! thanks!

For starters, you can initialize your array like this:

int[] values = {1,4,9,16,17,21,25,33,37,40};

You can use "values.length" to get the number of elements in the array. Use that in conjunction with a FOR loop and a temp variable to swap the values in the array in reverse order.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You could also use a stack to reverse the order. Put them in a stack, then pop them, and for each pop, put the element that was popped into an array. The array you end up with is the original one but in reverse order....

int[] values = new int[10];
values[ 10 ] = 0;

Huge amount of code so far ^_^.

IndexOutOfBoundsException You get here. Remember that, then int You pass in constructor is the number of elements, not the maximum index You can put element on.

Remember that:
new int[ length ] => index' from 0 to length-1.

Well, it clearly seems to me that you haven't put any effort yourself to solve the problem and hence you've written virtually nothing. I mean, you haven't figured out that you need to use a loop in order to change the whole array, let alone what to do in the loop.

Here's a hint: (think about it). It's just one approach. simple problems can be solved in many different and correct ways.
use a loop which goes from 0 to some index. (that some index is not the last element of the array). In each iteration of the loop, swap the first and last element.
You need to UNDERSTAND two things:
* if you assign the value of the first to last or vice versa, you'd lose data of one of them which you'd need later. So, you need to devise some technique to temporarily save the current element somewhere and yet change the values.
* if you change the value of the first and last and go till the last index, you would end up swapping the first with the last and the last with the first, which will be the original array again.

Please try it now. I hope I helped.

P.S.: Sorry for making my post a bit too lengthy.

right guys, ive made some progress on it.

the only problem i have now is how to reverse the numbers in descending order.

public class ArrayExercise1 {
    public static void main(String[] args) {
        int[] values = new int[10];

        values[ 0 ] = 1;
        values[ 1 ] = 4;
        values[ 2 ] = 9;
        values[ 3 ] = 16;
        values[ 4 ] = 17; 
        values[ 5 ] = 21;
        values[ 6 ] = 25;
        values[ 7 ] = 33;
        values[ 8 ] = 37;
        values[ 9 ] = 40;


        for(int count = 0; count < values.length; count++) {

        System.out.println(values[ count ]);

    }
}

I have that so far and it shows the number from smallest to largest, now im stuck on how to get them reversed.

Please get back to me! thanks

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Abbie - try to be nice.

Ramborambo:

Use another for loop, but iterate backwards through the array. As you iterate backwards, add each element to a different array. Use this for loop, and inside the for loop, add code that will put the element at the current index into a different array. If you do a search in the Java forum, you'll find that someone asked an almost identical question to the one you asked.

for (int i = array.length-1; i >=0; i--)
This article has been dead for over six months. Start a new discussion instead.