Do not understand code.

involves methods and arrays

Page 1 of 1

4 Replies - 451 Views - Last Post: 26 October 2009 - 09:36 PM Rate Topic: -----

#1 jparkerson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 09

Do not understand code.

Posted 26 October 2009 - 09:03 PM

We were given practice tests in class and were given the answers after taking the test. I have the answer to the question but do not understand how I got it. This is not a homework assignment it is for my own studying and I have a test tomorrow morning and still do not understand what is going on. If someone could explain what is happening line by line it would be incredibly helpful. Thanks here it is. The question that is being asked is: "What is the output of this code? Simulate the code and write down what happens in memory? Answer: 9 17 8 i do understand though what is happening in memory. Thanks

public class Rev 
{
	public static void main (String[] args) 
	{
	int[] numbers = {8, 17, 9};
	numbers = rev(numbers);
	for(int val : numbers) System.out.println(val);
	}


	public static int[] rev(int[] numbers)
	{
	int[] revNumbers = new int[numbers.length];
		for( int i = 0; i<numbers.length; i++)
		{
			revNumbers[revNumbers.length-i-1] = numbers[i];
		}
	
	return revNumbers;
	}
}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Do not understand code.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Do not understand code.

Posted 26 October 2009 - 09:09 PM

:code:
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5307
  • View blog
  • Posts: 27,208
  • Joined: 10-May 07

Re: Do not understand code.

Posted 26 October 2009 - 09:17 PM

View Postjparkerson, on 26 Oct, 2009 - 10:03 PM, said:

I have the answer to the question but do not understand how I got it.
...
If someone could explain what is happening line by line it would be incredibly helpful.

Sorry, but I don't see how that's possible. You obtained this code maybe, but how could you get the answer right, but not understand it?
Was This Post Helpful? 0
  • +
  • -

#4 jparkerson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 09

Re: Do not understand code.

Posted 26 October 2009 - 09:22 PM

We took the practice test in the last class. I did not fill out this question when I took it then after he explained all of the questions to the test and gave us the answers that is how I got the answer but still do not understand it. I swear I am not trying to get HW completed online. I am trying to understand how the teacher got this answer since we have a test tomorrow at 8 am.
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Do not understand code.

Posted 26 October 2009 - 09:36 PM

it would be best if you just get a piece of paper and work it out with each number. For example

revNumbers.length = numbers.length 3
revNumbers Means that the variable i is the element it is at, which it has 3 elements because that is the length.

Your line of code that confuses you is this:
revNumbers[revNumbers.length-i-1] = numbers[i];
if you take the first number in the loop, 0 and use it in that code you would get this.
[i]revNumbers[3-0-1] = numbers[0]
Break it down more,
revNumbers[3-0-1] will be revNumbers[2], which is element 2.
numbers[0] is the value that is in the original array. the first element in the array is going into the Last element in the Reversed Array.
Do that for all the numbers and if its still unclear.. idk :)

Hope this helps!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1