Jump to content

Help! Stuck with my project(Nested loop)

- - - - -

  • Please log in to reply
3 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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.

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

-Michael Sinz

#2
Sinipull

Sinipull

    Programming Expert

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

#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
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

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

-Michael Sinz

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
You were probably calling the method twice.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users