简体   繁体   中英

UnsupportedOperationException when adding class to Collection

Im trying to create a carpark program which simulates when cars are added to the carpark.

My idea is to create a list of type car which adds the car to a list of fixed capactiy (15).

Everything was working fine until I suddenly started getting the following exception:

Exception in thread "main" java.lang.UnsupportedOperationException

Im quite confused as to why this is occurring now because I havent changed anything and the exception seems to come and go.

I use netbeans and before this i was having issues with the main class not being recognised within the source package.

I can list the code for you below:

Carpark Class

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() );
    }
}

Car Class

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 (Enum class)

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 (Enum class)

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;
    }
}

I have read while conducting research that the package issue as mentioned above can occur due to low memory and I wonder if that is linked with my current problem?

Thanks in Advance

The problem is here:

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

The List implementation returned by Array#asList doesn't support add/remove operations. Just initialize your list with ArrayList :

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

of if you're using Java7+

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

My idea is to create a list of type car which adds the car to a list of fixed capactiy (15).

Add a condition in initialise method where if the spaces list contains a specific size (in this case, 15 ) then it won't be possible to add any more elements.

Plain vanilla Java doesn't offer such List where you could define a fixed size of elements. Still, this is provided by FixedSizeList class, which is available at Commons Collections Apache Library .

More info:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM