ArrayList错误程序将无法运行

时间:2013-12-07 00:52:10

标签: java eclipse arraylist

好的,所以我对java非常新。我正在努力为一堂课完成作业但是我被卡住了。我一直在主要的数组列表中收到错误,所以它不会运行,也不知道为什么或如何解决它。错误说“语法错误,参数化类型仅在源级别为5.0时可用”和“类型ArrayList不是通用的;它不能使用参数进行参数化”。我班上的某个人已经建议改变一些名为“合规”的东西,但是后来它开始说我的代码中没有主要内容,即使有明确的内容。我在学校的Citrix服务器上使用Eclipse。希望你们能帮助我解决问题,以便我可以运行代码。请帮我。该网站不会让我发布错误的屏幕截图,但它是在这些行....

ArrayList<Double>priceList = new ArrayList<Double>();
ArrayList<Double> quantityList = new ArrayList<Double>();
ArrayList<Double> amountList = new ArrayList<Double>();

这是节目描述.....

编写一个名为ThreeArrayLists.java的程序,该程序声明由名为priceList,quantityList和amountList的对象引用的三个ArrayList对象。每个ArrayList都应该在main()中声明,并且应该能够保存至少10个双精度数。    应存储在priceList中的数字是10.62,14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98。    应存储在quantityList中的数字是4,8.5,6,7.35,9,15.3,3,4.4,2.9 4.8。    您的程序应该将对这三个ArrayList对象的对象引用传递给名为extend()的方法,该方法应该将amountListArrayList中的元素计算为priceList和quantityListArrayList中相应元素的乘积,例如amountList.add(priceList.get) (i)* quantityList.get(i))。    Afterextend()已将值放入amountListArrayList对象中,创建一个显示所有三个列表结果的方法。需要使用适当的格式化技术来生成格式化的输出。

...以下是完整的代码。

package week6;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class ThreeArrayLists extends javax.swing.JFrame {
static String result;

public ThreeArrayLists() {
    initComponents();
}

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    ProceTextField = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    QuantityTextField = new javax.swing.JTextField();
    CalculateButton = new javax.swing.JButton();
    jLabel4 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    ResultTextArea = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("ThreeArrayLists");

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel1.setText("ThreeArrayLists");

    jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); 
    jLabel2.setText("priceList:");

    ProceTextField.setEditable(false);
    ProceTextField.setText("{ 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 }");

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); 
    jLabel3.setText("quantityList:");

    QuantityTextField.setEditable(false);
    QuantityTextField.setText("{ 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 }");

    CalculateButton.setText("Calculate");
    CalculateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            CalculateButtonActionPerformed(evt);
        }
    });

    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
    jLabel4.setText("amountList:");

    ResultTextArea.setColumns(20);
    ResultTextArea.setRows(5);
    jScrollPane1.setViewportView(ResultTextArea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(26, 26, 26)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(78, 78, 78)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 287, Short.MAX_VALUE))))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(QuantityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ProceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)))
            .addGap(46, 46, 46))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(235, Short.MAX_VALUE)
            .addComponent(CalculateButton)
            .addGap(199, 199, 199))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(208, Short.MAX_VALUE)
            .addComponent(jLabel1)
            .addGap(194, 194, 194))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(26, 26, 26)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(ProceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(30, 30, 30)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(QuantityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(32, 32, 32)
            .addComponent(CalculateButton)
            .addGap(21, 21, 21)
            .addComponent(jLabel4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();
}

private void CalculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    ResultTextArea.setText(result);
}

public static void main(String args[]) {
DecimalFormat df = new DecimalFormat("###,###.00");
double[] PRICE_ARRAY = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
double[] QUANTITY_ARRAY = { 4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8 };
ArrayList<Double>priceList = new ArrayList<Double>();
ArrayList<Double> quantityList = new ArrayList<Double>();
ArrayList<Double> amountList = new ArrayList<Double>();

for (int i=0; i < 10; i++){
    priceList.add(new Double(PRICE_ARRAY[i]));
    quantityList.add(new Double(QUANTITY_ARRAY[i]));
}

result = "";
for (int i = 0; i < 10; i++) {
    amountList.add((Double.valueOf(priceList.get(i).toString())) * (Double.valueOf(quantityList.get(i).toString())));
    result += String.valueOf(i+1) + ")  "
           + new String(df.format(priceList.get(i))) + " * "
           + new String(df.format(quantityList.get(i))) + " = "
           + new String(df.format(amountList.get(i))) + "\n";
}
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ThreeArrayLists().setVisible(true);
        }
    });
}

private javax.swing.JButton CalculateButton;
private javax.swing.JTextField ProceTextField;
private javax.swing.JTextField QuantityTextField;
private javax.swing.JTextArea ResultTextArea;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;

}

2 个答案:

答案 0 :(得分:1)

在我看来,您使用的JDK可能低于1.5版本,在Java 5之前不会引入泛型。

您可以尝试声明您的列表:

ArrayList priceList = new ArrayList();

然后,如果您想稍后操作数据,则需要记住在引用元素时强制转换为Double

答案 1 :(得分:0)

版本低于 1.5的

Java 不处理泛型类型(也称为其他类型参数化的类型:List)。

要在 Java 1.4中运行代码,您需要松开泛型类型参数并自行执行转换。贝娄是一种如何实现类似于你的目标的方式。

示例:

import java.util.ArrayList;

public class test {
    String result;


     double[] PRICE_ARRAY = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
     double[] QUANTITY_ARRAY = { 4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8 };
     ArrayList priceList = new ArrayList();
     ArrayList quantityList = new ArrayList();
     ArrayList amountList = new ArrayList();
     public void homework(){ 

         for (int i=0; i < 10; i++){
             this.priceList.add((Double)(PRICE_ARRAY[i]));
             this.quantityList.add((Double)(QUANTITY_ARRAY[i]));
             this.amountList.add((Double)priceList.get(i) * (Double)quantityList.get(i));
         }

     }
     public void output(){
         System.out.println("Price list: " + priceList.toString());
         System.out.println("Quantity list: " + quantityList.toString());
         System.out.println("Amount list: " + amountList.toString());
     }
}