(简单)向ArrayList <class> </class>添加不同的类型

时间:2014-02-07 02:14:57

标签: java arrays arraylist constructor int

所以我需要将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;
 }

1 个答案:

答案 0 :(得分:0)

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

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