943,829 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 462
  • Java RSS
May 11th, 2009
0

Help on arrays!!

Expand Post »
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!
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
ramborambo is offline Offline
5 posts
since May 2009
May 11th, 2009
0

Re: Help on arrays!!

Click to Expand / Collapse  Quote originally posted by ramborambo ...
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.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
May 11th, 2009
0

Re: Help on arrays!!

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....
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 12th, 2009
0

Re: Help on arrays!!

Click to Expand / Collapse  Quote originally posted by ramborambo ...
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.
Last edited by Zibo; May 12th, 2009 at 2:39 am.
Reputation Points: 12
Solved Threads: 3
Light Poster
Zibo is offline Offline
41 posts
since May 2009
May 12th, 2009
0

Re: Help on arrays!!

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
abbie is offline Offline
6 posts
since May 2009
May 12th, 2009
0

Re: Help on arrays!!

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
Reputation Points: 9
Solved Threads: 0
Newbie Poster
ramborambo is offline Offline
5 posts
since May 2009
May 12th, 2009
0

Re: Help on arrays!!

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.
Java Syntax (Toggle Plain Text)
  1. for (int i = array.length-1; i >=0; i--)
Last edited by BestJewSinceJC; May 12th, 2009 at 9:07 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: find multiple largest numbers
Next Thread in Java Forum Timeline: resetting paint graphics





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC