编译时被赋予非法的类型启动

时间:2016-01-08 21:25:23

标签: java arraylist

我对java编程很新,对数组列表这样的概念也不熟悉。我正在为课堂写一个程序,但我已经坚持这个问题几天了。当我编译此代码时,我收到“非法启动类型”。我花了几个小时看着它并试图找出它。 我在胡萝卜处接受了这个问题

 {  
     System.out.print(CO2<i>.gasWaste^());   
 }

gasWaste是CO2Footprint类中的一个方法,它根据arraylist中给出的信息返回double。 任何帮助将不胜感激。

import java.util.*;
/**
* Write a description of class CO2FootprintTester here.
* 
* @author (Austin J) 
* @version (1/4/2016)
*/
public class CO2FootprintTester
{
    public static void main(String [] args)
    {
        ArrayList <CO2Footprint> CO2 = new ArrayList<CO2Footprint>(5);


        CO2.add(new CO2Footprint(false, true, true, false,1300,.08,4, 1, 450));
        CO2.add(new CO2Footprint(true, true, true, false, 1200, .07, 3, 6, 400));
        CO2.add(new CO2Footprint(true, true, false, false, 1350, .09, 4, 4,600));
        CO2.add(new CO2Footprint(false, false, false, false, 1400, .1,5,1, 550));
        CO2.add(new CO2Footprint(true, true, true, true, 1100, .06, 3, 10, 450));

        for (int i = 0; i < 5; i++)
        {
            System.out.print(CO2<i>.gasWaste());
        }       
    }
}

2 个答案:

答案 0 :(得分:4)

我认为您的问题是CO2< i>,您是否被迫使用:

for (int i = 0; i < 5; i++)

如果没有,你可以在下面使用ArrayList的语法,它就可以了。

for(CO2Footprint co2Item: CO2){
    System.out.print(co2Item.gasWaste());
}

答案 1 :(得分:3)

你应该使用

   System.out.print(CO2.get(i).gasWaste());

而不是

   System.out.print(CO2<i>.gasWaste());

在您的完整示例中:

import java.util.*;
/**
* Write a description of class CO2FootprintTester here.
* 
* @author (Austin J) 
* @version (1/4/2016)
*/
public class CO2FootprintTester
{
   public static void main(String [] args)
   {
      ArrayList <CO2Footprint> CO2 = new ArrayList<CO2Footprint>(5);


      CO2.add(new CO2Footprint(false, true, true, false,1300,.08,4, 1, 450));
      CO2.add(new CO2Footprint(true, true, true, false, 1200, .07, 3, 6, 400));
      CO2.add(new CO2Footprint(true, true, false, false, 1350, .09, 4, 4,600));
      CO2.add(new CO2Footprint(false, false, false, false, 1400, .1,5,1, 550));
      CO2.add(new CO2Footprint(true, true, true, true, 1100, .06, 3, 10, 450));

      for (int i = 0; i < 5; i++)
      {
          System.out.print(CO2.get(i).gasWaste());
      }       
    }
}