473,434 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

How do I turn this for loop into a for each loop?

Mary Seelye
Expand|Select|Wrap|Line Numbers
  1. public void increaseVolume2()
  2.   {
  3.     SoundSample[] sampleArray = this.getSamples();
  4.     SoundSample sample = null;
  5.     int value = 0;
  6.  
  7.     //loop through all the samples in the array
  8.     for (int i = 0; i < sampleArray.length; i++)
  9.     {
  10.       sample = sampleArray[i];
  11.       value = sample.getValue();
  12.       sample.setValue((int) (value * 2));
  13.  
  14.     }
  15.   }
Oct 26 '10 #1
3 2039
Monomachus
127 Expert 100+
Well I found using google this link:
Using Foreach Loops in J2SE 1.5

So you need to implement Iterable interface and create an Iterator method for your class. In your case it should be SoundSample.
Expand|Select|Wrap|Line Numbers
  1. class Catalog implements Iterable<Product> {
  2.    private List<Product> products = new ArrayList<Product>();
  3.    void add(Product product) {
  4.       products.add(product);
  5.    }
  6.  
  7.    public Iterator<Product> iterator() {
  8.       return products.iterator();
  9.    }
  10. }
  11.  
  12. Catalog catalog = new Catalog();
  13.  
  14. catalog.add(new Product("1", "pinto", new BigDecimal("4.99")));
  15. catalog.add(new Product("2", "flounder", new BigDecimal("64.88")));
  16. catalog.add(new Product("2", "cucumber", new BigDecimal("2.01")));
  17.  
  18. for (Product product: catalog)
  19.    product.discount(new BigDecimal("0.1"));
  20.  
  21. for (Product product: catalog)
  22.    System.out.println(product);
  23.  
Oct 26 '10 #2
I'm not quite sure what you're doing here.
I need it to stay as close to what I have as possible.
Oct 26 '10 #3
I think I figured it out.

This is what I have now and it works just fine:
Expand|Select|Wrap|Line Numbers
  1. public void increaseVolume3()
  2.   {
  3.     SoundSample[] sampleArray = this.getSamples();
  4.     int value = 0;
  5.  
  6.     //loop through all the samples in the array
  7.        for (SoundSample sample : sampleArray)
  8.     {
  9.  
  10.       value = sample.getValue();
  11.       sample.setValue((int) (value * 2));
  12.  
  13.     }
  14.   }
Oct 26 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tjerk Wolterink | last post by:
Hello all, i have a xsl:for-each loop like this: <xsl:for-each select="xc:agendapunt"> <xsl:sort select="xc:datum/xc:year" data-type="number"/> <xsl:sort select="xc:datum/xc:month"...
2
by: martin.tschofen | last post by:
How do I select only the first node from a for-each loop that contains a the element "photo". the following xsl finds all "art" elements that have a "photo" element. The problem is that I only...
5
by: Alex | last post by:
hi, I have a class that has inheritted a collectionbase class The class works fine generally, I can add items to the collection however whenever I execute a For Each loop, the dispose event is...
2
by: bunnyman | last post by:
I have a for each loop in javascript, of which I need to output to an ASP array. unfortunantly not too familiar with javascript.. the loop is for items ordered in a shopping cart. they are...
3
by: deko | last post by:
Problem: why doesn't this With block work? is it possible to have a For Each loop within a With block? With objWord .Documents.Add Template:=strTemplate, NewTemplate:=False, DocumentType:=0...
5
by: B. Chernick | last post by:
Is this a bug or just bad programming? I've never encountered this problem before. (Bare minimum sample form to illustrate.) I've also tried this with a class. Same result. The error is: For...
3
by: AutoShutdown | last post by:
How can I change a FOR EACH loop to a DO WHILE loop? I have this code... For Each objFile In objFolder.Files response.write objFile.Name Next
3
by: nina297 | last post by:
Good morning, How do I set up a For each loop to loop through my records in the database? My fields are topics, question and answers. I want the one topic displayed that goes with each...
7
by: morrisp | last post by:
Newbi here, How do I find a specific form control without using a For Each loop? While the loop works to find a control, it seems to have overhead I'm trying to avoid. I'm using A2K. Here's the...
5
by: kimiraikkonen | last post by:
Hi, I'm trying to do a batch process for all the items in my listbox1 using "for each...next" ? I'm not well-experienced on this yet, how can i sample this? For example: From item1 to last item...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.