【问题标题】:Java ArrayList compiling on machine but not on school systemJava ArrayList 在机器上编译但不在学校系统上
【发布时间】:2013-11-10 20:01:09
【问题描述】:

我的任务是创建一个 ArrayList 来跟踪商店每天的销售情况并发布客户花费最多的帖子。我完成了任务,所以我想。所有三个类都在 Eclipse 和我的终端窗口中编译。但是,当我将它们上传到学校的系统然后进行测试时,我在课堂上遇到了错误。不用说这对我来说是令人沮丧和困惑的,我是一个非常陌生的人。这是我的 ArrayList 语法的某种问题,但我无法弄清楚,因为它在 Eclipse 上运行良好。 :/

我得到的错误是:

Store.java:17: illegal start of type
customers = new ArrayList<>(); \\the up arrow is under the ">"

代码如下。

//******************************************
// Store.java 
// Written...
// 2013
//*******************************************

//===========================================
// Store class holds ArrayList of customers
//===========================================

import java.util.ArrayList;
import java.util.List;

public class Store {private List<Customer> customers; //stores customers

public Store() {
    customers = new ArrayList<>();
}

//creates new customer for arraylist
public void addSale(String customerName, double amount) {
    Customer c = new Customer(customerName, amount);
    customers.add(c);
}

//finds the best customer
public String nameOfBestCustomer() {
if (customers.isEmpty()) {
    return "No customers are stored!";
} else {
    Customer best = customers.get(0);

    for (Customer c : customers) {
        if (c.getAmount() > best.getAmount()) {
            best = c;
        }
    }
    return best.getName();
}
}

}

【问题讨论】:

    标签: java eclipse arraylist compiler-errors


    【解决方案1】:

    您在家中使用 Java 7,在学校使用 Java 6 进行编译,因为菱形运算符仅对 Java 7 有效。

    解决方案:两者都使用 Java 7,或者确保您的代码与 Java 6 兼容。

    即,改变这个:

    customers = new ArrayList<>();
    

    到这里:

    customers = new ArrayList<Customer>();
    

    【讨论】:

    • @phantasms:很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多