wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array with for nested loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array with for nested loop" Watch "Array with for nested loop" New topic
Author

Array with for nested loop

Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Can anybody tell me how for nested loop work with an array.
Thanks in advance
[This message has been edited by Sadaf Zaidi (edited October 31, 2001).]
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
you can use for loops to fill the array:
for (int j = 0; j < myArray.length; j++)
myArray[j] = whatever;
now using nested for loops, I would only assume you have a 2 dimensional array to fill:
for (int column = 0; column < myArray.length; column++)
for (int row = 0; row < myArray[column].length; row++)
myArray[column][row] = whatever;
Is that what you're asking?
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
[This message has been edited by Michael Bruesch (edited October 29, 2001).]
[This message has been edited by Michael Bruesch (edited October 29, 2001).]


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Sadaf
Another common use of nested for loops with an array is when you want to compare all elements of the array to each other. Is this the problem you wish to solve?
Michael
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Originally posted by Michael Fitzmaurice:
Hi Sadaf
Another common use of nested for loops with an array is when you want to compare all elements of the array to each other. Is this the problem you wish to solve?
Michael

Thanx alot for your reply Michael you are right I wants to compare all elements of the array to each other.Can u please tell me how to compare array elements to each other.
[This message has been edited by Sadaf Zaidi (edited October 31, 2001).]
[This message has been edited by Sadaf Zaidi (edited November 01, 2001).]
sami syed abdul
Greenhorn

Joined: Nov 02, 2001
Posts: 2
hello forum i have just started learning java any advice as i plan to atampt SCJP IN FOUR MONTHS
thanks
a.sami
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

a. sami,
You would want to go to the
Programmer Certification Study forum for SCJP study questions.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform


Junilu - [How to Ask Questions] [How to Answer Questions]
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Originally posted by Sadaf Zaidi:
Thanx alot for your reply Michael you are right I wants to compare all elements of the array to each other.Can u please tell me how to compare array elements to each other.

Certainly. Have a look at the code below, which creates an array of integers and compares all elements to each other. Sorry it took me a while to reply - I have been offline for a few days.
<code><pre>
public class ArrayComparison
{
public static void main(String[] args)
{
int[] bunchOfNumbers = {1, 2, 3, 4, 1, 2, 3, 4};

// compare each array element to all others
int numberOfElements = bunchOfNumbers.length;

for(int i = 0; i < numberOfElements; i++)<br /> {<br /> int current = bunchOfNumbers[i];<br /> <br /> for(int j = (numberOfElements - 1); j > i; j--)
{
int numberToMatch = bunchOfNumbers[j];

if(current == numberToMatch)
{
System.out.println( "Element " + i + " (" + current + ") = Element "
+ j + " (" + numberToMatch + ")" );
}
}
}
}
}
</pre></code>

Hope this helps
Michael

------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley
[This message has been edited by Michael Fitzmaurice (edited November 03, 2001).]
Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Thank You Michael its clear now.
 
jQuery in Action, 2nd edition
 
subject: Array with for nested loop