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
posted
0
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
posted
0
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
posted
0
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
posted
0
hello forum i have just started learning java any advice as i plan to atampt SCJP IN FOUR MONTHS thanks a.sami
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];
------------------ "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).]