【问题标题】:UnsupportedOperationException when adding class to Collection将类添加到 Collection 时出现 UnsupportedOperationException
【发布时间】:2014-11-23 19:10:36
【问题描述】:

我正在尝试创建一个停车场程序,用于模拟何时将汽车添加到停车场。

我的想法是创建一个类型的汽车列表,将汽车添加到固定容量 (15) 列表中。

一切正常,直到我突然开始收到以下异常:

线程“主”java.lang.UnsupportedOperationException 中的异常

我很困惑为什么现在会发生这种情况,因为我没有改变任何东西,而且异常似乎来来去去。

我使用 netbeans,在此之前我遇到了源包中无法识别主类的问题。

我可以在下面为您列出代码:

停车场类

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class CarPark 
{
    List<Car> spaces = Arrays.asList( new Car[15] ); //Set max size of list to be 15
    Scanner scanner = new Scanner( System.in );

    public void initialise()
    {
        CarSize  carSize  = null;
        CarValue carValue = null;

        System.out.println("Please enter the registration Number");
        String regNo = scanner.nextLine();

        System.out.println("\nNow the size of the car");
        System.out.println("0 - Small");
        System.out.println("1 - Medium");
        System.out.println("2 - Large");
        int size = scanner.nextInt();
        switch( size )
        {
            case 0: carSize = CarSize.Small;  break;
            case 1: carSize = CarSize.Medium; break;
            case 2: carSize = CarSize.Large;  break;   
        }

        System.out.println("\nFinally the value of the car");
        System.out.println("0 - Loq");
        System.out.println("1 - Medium");
        System.out.println("2 - High");
        int value = scanner.nextInt();
        switch( value )
        {
            case 0: carValue = CarValue.Low;    break;
            case 1: carValue = CarValue.Medium; break;
            case 2: carValue = CarValue.High;   break;   
        }

        addCar(regNo, carSize, carValue);
        showTotalCars();
    }

    //Adds the car to the list
    public void addCar( String regNum, CarSize size, CarValue value  )
    {
        Car car = new Car(regNum, size, value);
        spaces.add(car);
    }

    public void showTotalCars()
    {
        System.out.println("Total Cars = " + spaces.size() );
    }
}

汽车类

public class Car 
{
    String RegistrationNumber = "";
    CarSize size = null;
    CarValue value = null;

    public Car( String regNo, CarSize sizeOfCar, CarValue valOfCar )
    {
        RegistrationNumber = regNo;
        size = sizeOfCar;
        value = valOfCar;
    }

    @Override
    public String toString()
    {
        return "Registration Number = " + RegistrationNumber
                + "\nSize = " + size.name()
                + "\nValue = " + value.name();
    }
}

CarSize(枚举类)

public enum CarSize 
{
    Small(0, "For small cars"),
    Medium(1, "For medium cars"),
    Large(2, "For large cars");

    private final int Id;
    private final String description;

    CarSize( int identifier, String descr )
    {
       this.Id = identifier;
       this.description = descr;
    }

    public int getId() 
    {
        return Id;
    }

    public String getDescription() 
    {
        return description;
    }
}

CarValue(枚举类)

public enum CarValue 
{
    Low(0, "For low value cars"),
    Medium(1, "For medium value cars"),
    High(2, "For high value cars");

    private final int Id;
    private final String description;

    CarValue( int identifier, String descr )
    {
        this.Id = identifier;
        this.description = descr;
    }

    public int getId() 
    {
        return Id;
    }

    public String getDescription() 
    {
        return description;
    }
}

我在进行研究时了解到,上述软件包问题可能是由于内存不足而发生的,我想知道这是否与我当前的问题有关?

提前致谢

【问题讨论】:

    标签: java netbeans collections


    【解决方案1】:

    问题出在这里:

    List<Car> spaces = Arrays.asList( new Car[15] );
    

    Array#asList 返回的List 实现不支持添加/删除操作。只需使用ArrayList 初始化您的列表:

    List<Car> spaces = new ArrayList<Car>();
    

    如果你使用的是 Java7+

    List<Car> spaces = new ArrayList<>();
    

    我的想法是创建一个汽车类型列表,将汽车添加到固定容量列表 (15)。

    initialise 方法中添加一个条件,如果spaces 列表包含特定大小(在本例中为15),则无法再添加任何元素。

    Plain vanilla Java 不提供这样的List,您可以在其中定义固定大小的元素。不过,这是由FixedSizeList 类提供的,可在Commons Collections Apache Library 获得。

    更多信息:

    【讨论】:

    • 在这种情况下,有没有办法将 arraylist 容量限制为 15?要求仅允许将 15 辆汽车添加到列表中。谢谢
    • @KyleT 不,没有办法。您应该在将元素添加到 List 的方法中手动执行此操作。
    • 非常好的答案,你在这个话题上给了我更多启发!谢谢。
    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多