2 Replies - 508 Views - Last Post: 08 March 2008 - 06:40 PM Rate Topic: -----

#1 Alrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-February 08

What is this code doing?

Posted 08 March 2008 - 06:13 PM

This code was provided in our assignment instructions

variable is a custom class, I can give details if it's important.

I'm pretty new to this and I haven't seen an array declared this way before.
I don't entirely understand what this code is doing. Am I right in thinking this code is creating an empty array with a size of 25 ('a' -> 'y')?

If I wanted to access an item in this array, could I use a letter value eg memory[q]?

			
variable[] memory = new variable['z'];
			for(int i=0; i<'z'; i++)
			{
				memory[i] = new variable();
			}




(I would ask the instructor but I have a lot of work still to do on this assignment and it is due the same day as the next lecture. I won't be able to get in touch with him before then)

This post has been edited by Alrec: 08 March 2008 - 06:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: What is this code doing?

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: What is this code doing?

Posted 08 March 2008 - 06:35 PM

You're almost right in that thinking. The data type char is a special integer, and can be used as such interchangeably. The character 'z' has the int value 122. That code is creating an array with 122 elements. If the code said:
variable[] memory = new variable['z' - 'a'];

It would have created an array with 26 elements. In some languages you can specify a starting index and length, but arrays in Java and C all start at zero. It would be syntactically cleaner to start an array at index of 'a' and go to index of 'z' but Java doesn't let you.
Was This Post Helpful? 0
  • +
  • -

#3 Alrec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-February 08

Re: What is this code doing?

Posted 08 March 2008 - 06:40 PM

Thanks, that helps a lot :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1