4 Replies - 1115 Views - Last Post: 10 July 2009 - 06:05 PM Rate Topic: -----

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 01-February 09

accesing array with while loop

Post icon  Posted 10 July 2009 - 05:12 PM

Hi,

i'm trying to access and print the days of the week with a while loop::

String daysOfWeek []= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
		while( 1 to daysOfWeek.lentgh)
		{
			System.out.println(daysOfWeek);
		}


can anyone help me on how should be the condition of the while loop??
tnx...

Is This A Good Question/Topic? 0
  • +

Replies To: accesing array with while loop

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: accesing array with while loop

Posted 10 July 2009 - 05:14 PM

for(String s : daysOfWeek)
	System.out.println(s);


this is called the enhanced for loop or for-each loop, coz it assigns each element in the array to s on each iteration..you could do it using a while loop like this..

int i = 0;
while(i < daysOfWeek.length)
{
	System.out.println(daysOfWeek[i]);
	i++;
}



first declare the counter variable i to 0 coz you're gonna be printing from the start of the array, arrays start from zero up to length-1, so you will be iterating till length-1

This post has been edited by mostyfriedman: 10 July 2009 - 05:18 PM

Was This Post Helpful? 1
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 516
  • View blog
  • Posts: 5,588
  • Joined: 20-March 08

Re: accesing array with while loop

Posted 10 July 2009 - 06:02 PM

View Postmil1234, on 10 Jul, 2009 - 06:12 PM, said:

String daysOfWeek []= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
		while( 1 to daysOfWeek.lentgh)
		{
			System.out.println(daysOfWeek);
		}


That almost looks like you've used Visual Basic before. But yes, Java (and C/C++/C#) are all of the same syntax, and they are all a bit more symbol oriented than VB.

It's almost more like a symbolic math comparison -- i < daysOfWeek.length -- i will be your control variable, whose only purpose is to count up and help us access the array in order. So...that translates to while i is less than the length of the daysOfWeek array.

int i = 0;

while (i < daysOfWeek.length)
{
    System.out.println(daysOfWeek[i]);
    i++; // simply increase the value of i by 1
}


Hope this clears it up! :)
Was This Post Helpful? 1
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: accesing array with while loop

Posted 10 July 2009 - 06:03 PM

hey dont copy my code brad..that's quality stuff there :P
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 516
  • View blog
  • Posts: 5,588
  • Joined: 20-March 08

Re: accesing array with while loop

Posted 10 July 2009 - 06:05 PM

View Postmostyfriedman, on 10 Jul, 2009 - 07:03 PM, said:

hey dont copy my code brad..that's quality stuff there :P


That code must have taken you hours, man. :P ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1