简体   繁体   中英

Java array list size issue

I'm currently working on a project for class but struggling with a bit of coding relating to ArrayList s. It is as yet unfinished code; however, when I'm working on the enrollstudent method I'm having an issue comparing the length of an ArrayList to the variable amountstudents .

Below is a copy of the code for the full class. There is another seperate class related but I don't think it's relevant here.

Any help would be greatly appreciated.

import java.util.*;
import java.util.Scanner;

public class Course {

    int amountstudents;
    String coursename;
    String level;
    ArrayList<String> students = new ArrayList<String>();
    String tutor;

    Scanner in = new Scanner(System.in);

    public Course(int MaxCapacity) {
        MaxCapacity = amountstudents;
        tutor = "Not set yet";
        coursename = "Not set yet";
        level = "Not set yet";
    }

    public void enrollstudent(String addstudent) {
        if(students.size > amountstudents) {
            System.out.println("Unfortunately the class is already full so you can not be enrolled at this time");
        }
        else {
            students.add(Student.fname);
        }
    }

    public void courselevel() {
        System.out.println("Please enter course level");
        level=in.next();
    }

    public void coursetitle() {
    }
}

It's students.size() , not students.size .

Also, there's another error:

It should be students.add(addstudent) , not students.add(Student.fname) .

它是students.size() ,因为students是一个ArrayList

You want to use students.size() . size is a method on List types, not a property (like length is on arrays).

Also, in the constructor, you have this reversed:

MaxCapacity = amountstudents;

it should be:

amountstudents = MaxCapacity;

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