7 Replies - 2330 Views - Last Post: 13 April 2013 - 05:16 PM Rate Topic: -----

#1 josephine11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

How to convert a for loop into an enhanced() for loop?

Posted 11 April 2013 - 07:09 PM

I have been looking to see the difference between a regular for loop and an enhanced for loop but I can't find an example, could someone help me convert the following into an enhanced() for loop?

 for (int i = 0; i < ball.length; ++i)
      {
     
         System.out.println("Ball #" + (i + 1) + " is a " + ball[i].getType());

         System.out.print("Playing: ");
         ball[i].play();

         if (ball[i] instanceof Bounceable)
         {
            System.out.println("Bouncing " + ((Bounceable) ball[i]).bounce());
         }
         else
         {
            System.out.println("You can't bounce this ball!");
         }
         System.out.println("\n");
      }



Is This A Good Question/Topic? 0
  • +

Replies To: How to convert a for loop into an enhanced() for loop?

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to convert a for loop into an enhanced() for loop?

Posted 11 April 2013 - 07:20 PM

ball[] is an array of what ?
Assuming it is an array of Xyz

int k = 0
for (Xyz xyz;  ball)
     {
    
        System.out.println("Ball #" + (++k) + " is a " + xyz.getType());

        System.out.print("Playing: ");
        xyz.play();

        if (xyz instanceof Bounceable)
        {
           System.out.println("Bouncing " + ((Bounceable) xyz).bounce());
        }
        else
        {
           System.out.println("You can't bounce this ball!");
        }
        System.out.println("\n");
     }




But is is kind of useless to use the "enhanced" format as you need the index toprint it

System.out.println("Ball #" + (++k) + " is a " + xyz.getType());
Was This Post Helpful? 1
  • +
  • -

#3 josephine11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: How to convert a for loop into an enhanced() for loop?

Posted 11 April 2013 - 08:28 PM

I still confused and cannot seem to convert the loop this is the whole program.
public class PolyTest
{
   public static void main(String[] args)
   {
      Ball[] ball = { new Baseball(), new Basketball(), new Bowlingball(),new Tennisball() };

      System.out.println("For each type of ball...\n");

      for (int i = 0; i < ball.length; ++i)
      {
     
         System.out.println("Ball #" + (i + 1) + " is a " + ball[i].getType());

         System.out.print("Playing: ");
         ball[i].play();

         if (ball[i] instanceof Bounceable)
         {
            System.out.println("Bouncing " + ((Bounceable) ball[i]).bounce());
         }
         else
         {
            System.out.println("You can't bounce this ball!");
         }
         System.out.println("\n");
      }
      System.out.println("\nEnd of program.");
   }
}

public abstract class Ball
{
	String type;

	public Ball(String type)
	{
		this.type=type;
	}

	public abstract void play();
	public abstract void basketball();
	public abstract void tennisball();
	public abstract void bowlingball();
	public abstract void baseball();


	public String getType()
	{
		return type;
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: How to convert a for loop into an enhanced() for loop?

Posted 12 April 2013 - 01:10 AM

Then don't do it in a "whole program". Let's say we have an array:

String[] alphabet = {"a", "b", "c"};


The most common thing we do with it in programming is process each element once:

for(int i = 0; i < alphabet.length; i++) {
    String s = alphabet[i];
    System.out.println(s);
}


Sometimes we really need the index i for something but often we just use it to get the correct item out of the array. The enhanced for loop just simplifies this case:

for(String s : alphabet) {
    System.out.println(s);
}


Here, s is the counter variable and on each iteration it takes the next item in the array. The enhanced for loop is great if you just need to touch each element in an array or collection once. If you need the counter variable, you are better off with the c-style for loop you are familiar with.
Was This Post Helpful? 2
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to convert a for loop into an enhanced() for loop?

Posted 12 April 2013 - 03:44 AM

      int i = 0;
      for (Ball b : ball)
      {
     
         System.out.println("Ball #" + (++i) + " is a " + b.getType());

         System.out.print("Playing: ");
         b.play();

         if (b instanceof Bounceable)
         {
            System.out.println("Bouncing " + ((Bounceable) B)/>.bounce());
         }
         else
         {
            System.out.println("You can't bounce this ball!");
         }
         System.out.println("\n");
      }
 

Was This Post Helpful? 1
  • +
  • -

#6 josephine11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-September 12

Re: How to convert a for loop into an enhanced() for loop?

Posted 13 April 2013 - 01:01 PM

View Postpbl, on 12 April 2013 - 05:44 AM, said:

      int i = 0;
      for (Ball b : ball)
      {
     
         System.out.println("Ball #" + (++i) + " is a " + b.getType());

         System.out.print("Playing: ");
         b.play();

         if (b instanceof Bounceable)
         {
            System.out.println("Bouncing " + ((Bounceable) B)/>/>.bounce());
         }
         else
         {
            System.out.println("You can't bounce this ball!");
         }
         System.out.println("\n");
      }
 
Thank you so much, I couldn't see how to write the counter variable, all I need was the "b".... question.. why is the variable shortened like this?

Was This Post Helpful? 0
  • +
  • -

#7 Kyran   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 12-July 12

Re: How to convert a for loop into an enhanced() for loop?

Posted 13 April 2013 - 03:02 PM

Do you mean the variable b is shortened? It can be any length,
for (Ball blkjhsldkjhflskjhdlkjhfsldkuhlkjhsflduhs : ball) {
System.out.println("Be gentle, these balls are sensitive");
}

if you really wanted. b is just easier to type when referencing the variable.
Was This Post Helpful? 1
  • +
  • -

#8 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: How to convert a for loop into an enhanced() for loop?

Posted 13 April 2013 - 05:16 PM

"b" is a placeholder name for a temporary variable. It needs a name to function, so we call it b for ball.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1