java 使用 for 循环遍历对象和方法列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10379685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:46:30  来源:igfitidea点击:

Iterate through list of objects & methods using for loop

javaobjectmethodsiterationaccumulator

提问by user1251814

The entirety of the code can be found at the bottom of this post, but the only part that really concerns me is,

整个代码可以在这篇文章的底部找到,但唯一真正让我担心的部分是,

for(zed = 0; zed<photoes.size(); zed++) {
totesPrice += photoes<zed>.getCost();
totesSize += photoes<zed>.getMegabytes();
totesSpeed = photoes<zed>.getSpeed();
}

Ideally the totesPrice, totesSize, and totesSpeed would be the sum of the get methods for the objects stored in the photoes list array.

理想情况下,totesPrice、totesSize 和 totesSpeed 将是存储在 photoes 列表数组中的对象的 get 方法的总和。

// Import classes for class
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class DigitalMain
{
  public static void main(String args[])
  {
    String cont = "Y";
    String heightString,width,bitpsString = "";
    String widthString = "";
    int zed;
    double bitps,pHS,pWS = 0.0;
    double totesSpeed = 0.0;
    double totesPrice = 0.0;
    double totesSize = 0.0;
    DecimalFormat wholeDigits = new DecimalFormat("0");
    DecimalFormat dec = new DecimalFormat("0.00");

    List<DigitalPhoto> photoes = new ArrayList<DigitalPhoto>();
    do
    {
    DigitalPhoto photo = new DigitalPhoto();
    heightString = JOptionPane.showInputDialog("Please enter height");
    pHS = Double.parseDouble(heightString);
    photo.setHeight(pHS);
    widthString = JOptionPane.showInputDialog("Please enter width");
    pWS = Double.parseDouble(widthString);
    photo.setWidth(pWS);
    JOptionPane.showMessageDialog(null, "Height: " + photo.getHeight() + "\nWidth: " + photo.getWidth() + "\nResolution: " + photo.getResolution() + " DPI\nCompression Ratio: " + photo.getCompression() + "\nRequired Storage: " + dec.format(photo.getKilo()) + " Kilobytes.\nPrice of Scanned Photo: $" + dec.format(photo.getCost()) + " dollars.");
    do
    {
    bitpsString = JOptionPane.showInputDialog("Please enter your internet connection speed in BITS not BYTES please.");
    bitps = Double.parseDouble(bitpsString);
    } while (bitps < 0 && bitps > 99999999);
    photo.setSpeed(bitps);

    for(zed = 0; zed<photoes.size(); zed++) {
    totesPrice += photoes<zed>.getCost();
    totesSize += photoes<zed>.getMegabytes();
    totesSpeed = photoes<zed>.getSpeed();
    }
    cont = JOptionPane.showInputDialog("\nType \'Y\' to try again or anything but \'Y\' to use values.");
    photoes.add(photo);
    } while (cont.equalsIgnoreCase("Y"));





    double seconds = transferTime(totesSize, totesSpeed);
    double minutes = seconds / 60;
    double realsec = seconds % 60;

    JOptionPane.showMessageDialog(null, "You will be paying: " + totesPrice + "\nRequired Storage is: " + totesSize + "Required time for transfer is: " + wholeDigits.format(minutes) + " minutes, and " + wholeDigits.format(realsec) + " seconds.");

  }

  public static double transferTime(double totalStorage, double netSpeed) {
    double bits, seconds;
    bits = (totalStorage * 8);
    seconds = (bits / netSpeed);
    return seconds;
    };
}

回答by Sumit Singh

I think u have to your generics.. for:each
u can try this ...

我想你必须对你的泛型.. for:each
你可以试试这个......

    for (DigitalPhoto digitalPhoto : photoes) {
        totesPrice += digitalPhoto.getCost();
        totesSize += digitalPhoto.getMegabytes();
        totesSpeed = digitalPhoto.getSpeed();
     }

回答by Mihai Andrei Rustiuc

You could use an Iterator instead :

您可以改用迭代器:

  Iterator it = photoes.iterator();
  while(it.hasNext())
  {
    DigitalPhoto temp = it.next();
    totesPrice += temp.getCost();
    totesSize += temp.getMegabytes();
    totesSpeed = temp.getSpeed();
  }

回答by Ravi Jain

for(zed = 0; zed<photoes.size(); zed++) 
{
   totesPrice += photoes.get(zed).getCost();
   totesSize += photoes.get(zed).getMegabytes();
   totesSpeed = photoes.get(zed).getSpeed();
}

Try this.

试试这个。