Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help! Stuck with my project(Nested loop)

nested loop loop

  • Please log in to reply
3 replies to this topic

#1 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 August 2011 - 12:04 AM

Hello,

Facing something weird with one method of my class

numHands=2; cardsPerHand=2;
hand is an ArrayList
public String[] cardToImage(){
		String[] cards = new String[numHands*cardsPerHand];
		int c = 0;
		for (int i=0; i < numHands; i++){
			System.out.println("For i = "+i);	
			for (int x=0; x < cardsPerHand; x++){
				System.out.println("For x = "+x);	
				cards[c]=hand.get(x).toString()+".jpg";	
				c++;
			}
		}
		return cards;
	}

RESULT:

For i = 0
For x = 0
For x = 1
For i = 1
For x = 0
For x = 1 [COLOR="#FF0000"]<-----[/COLOR]
For i = 0
For x = 0
For x = 1
For i = 1
For x = 0
For x = 1

Also, how I can use the returned cards to another class and gain access to a specific element?

Thank you

Edited by toto_7, 12 August 2011 - 03:36 AM.

  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 12 August 2011 - 03:18 AM

Well, you didn't describe the problem....
What were you trying to achieve instead of that output?
  • 0

#3 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 August 2011 - 03:42 AM

Well if you check the output, you will see that printed twice. After sixth line, happening again the same thing

---------- Post added at 12:42 PM ---------- Previous post was at 12:38 PM ----------

SOLVED
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 12 August 2011 - 04:09 AM

You were probably calling the method twice.
  • 0





Also tagged with one or more of these keywords: nested loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download