【问题标题】:(Simple) Add different types to ArrayList <class>(简单)向 ArrayList <class> 添加不同的类型
【发布时间】:2014-02-07 02:14:56
【问题描述】:

所以我需要将 int 和 boolean 输入到承包商中,以便我可以通过方法计算不同的东西。有没有办法通过一个或两个 ArrayList 来做到这一点。我只是需要帮助才能到达那里! 感谢您的帮助。

主要方法:

import java.util.ArrayList;
public class Tester
{
public static void main(String[] args)
{
   ArrayList<Contructor> cO2 = new ArrayList<Constructor>();
       // add households to the arraylist
       CO2FromWaste homeData = new CO2FromWaste();
       cO2.add();

   for(Contructor dataRecord : cO2)
   {
       dataRecord.calcGrossWasteEmission();
       dataRecord.calcWasteReduction();
       dataRecord.calcNetWasteReduction();
   }

构造函数:

public class Contructor
{
private int myNumPeople;
private boolean myPaper, myPlastic, myGlass, myCans;
private double myEmissions, myReduction, myNetEmissions;

/**
* Constructor for objects of type CO2FromWaste
* @param numPeople number of people in a household
* @param paper whether or not paper is recycled
* @param plastic whether or not plastic is recycled
* @param glass whether or not glass is recycled
* @param cans whether or not cans are recycled
*/
Contructor(int numPeople, boolean paper, boolean plastic, boolean glass, boolean cans)
{
   myNumPeople = numPeople;
   myPaper = paper;
   myPlastic = plastic;
   myGlass = glass;
   myCans = cans;
   myEmissions = 0.0;
   myReduction = 0.0;
   myNetEmissions = 0.0;
 }

【问题讨论】:

  • 您的问题是什么?如何将CO2FromWaste 添加到ArrayList
  • 是的,这是我的问题
  • 检查我的答案是否是你要找的...

标签: java arrays arraylist constructor int


【解决方案1】:

你快到了。 ArrayList#add 方法将 Object 作为参数。在这种情况下,您的 ObjectCO2FromWaste

ArrayList<CO2FromWaste> cO2 = new ArrayList<CO2FromWaste>();
CO2FromWaste homeData = new CO2FromWaste();
cO2.add(homeData);

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多