简体   繁体   English

将增量int分配给arraylist中的元素,Java

[英]Assigning an incrementing int to an element in an arraylist, Java

So I'm still fairly new to Java and it's also been a while since I've programmed in Java. 因此,我对Java还是很陌生,而且自从我用Java编程以来已经有一段时间了。 What I want know is how to assign an integer, like a student number, to an element in an array list. 我想知道的是如何为数组列表中的元素分配一个整数,例如学生编号。 I've got three elements in my arraylist, and each element has to have a 7 digit student number with the 7th digit as the next number in sequence (ex. el 1 with student number 1234567, el 2 with 1234568, el 3 with 1234569). 我的数组列表中有三个元素,每个元素必须有一个7位数的学生编号,其中第7个数字是序列中的下一个数字(例如el 1的学生编号为1234567,el 2的1234568,el 3的1234569 )。 I'm not sure if I'm on the right track or not. 我不确定自己是否走对了。 This code is a work in progress and there are many things I still have to add or fix. 这段代码正在进行中,还有很多事情我仍要添加或修复。 My arraylist is in the next code block titled StudentApp. 我的数组列表位于标题为StudentApp的下一个代码块中。

public class Student    {

    //fields
    private String firstName;
    private String lastName;
    private int sNumber;
    private String major;
    private double gpa;

    private static int count()  {
        int count = 1234567;
        for (int i = 1; i <= count; i++)    {
            System.out.print(count);
        }
        return count;
    }

    //constructors
    public Student()    {
        firstName = null;
        lastName = null;
        sNumber = 1234567;
        major = null;
        gpa = 0.0;

    }

    public Student(String fName, String lName, int sNumber, String maj, double gpa) {
        firstName = fName;
        lastName = lName;
        sNumber = sNumber;
        major = maj;
        gpa = gpa; 
    }

    //methods

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getsNumber() {
        for(int sNumber = 0; sNumber >= 7; sNumber++)   {
            int sNumber = 1234567;
            sNumber++ 
        }
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    @Override
    public String toString() {
        return "S" + sNumber + " " + firstName + " "+ lastName + " " + "(" + major +")" + " " + "gpa:" + gpa;
    }
}

import java.util.Scanner;
import java.util.ArrayList;

public class StudentApp {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Student> names = new ArrayList<Student> ();

        Student student1 = new Student();
        student1.setFirstName("Brian");
        student1.setLastName("Scholl");
        student1.setMajor("CS");
        student1.setGpa(3.5);

        Student student2 = new Student();
        student2.setFirstName("Emily");
        student2.setLastName("Davies");
        student2.setMajor("ME");
        student2.setGpa(3.7);

        Student student3 = new Student();
        student3.setFirstName("Sarah");
        student3.setLastName("Dixon");
        student3.setMajor("EE");
        student3.setGpa(3.8);

        names.add(student1);
        names.add(student2);
        names.add(student3);

        int choice;
        do{
            displayMenu();
            choice = input.nextInt();
            switch(choice)  {
            case 1:
                Student student4 = new Student();

                System.out.print("First name: ");
                String fName1 = input.nextLine();
                System.out.println();
                System.out.print("Last name: ");
                String lName1 = input.nextLine();
                System.out.println();

                System.out.print("Major: ");
                String major1 = input.nextLine();
                System.out.println();
                System.out.print("GPA: ");
                double gpa1 = input.nextDouble();

                student4.setFirstName(fName1);
                student4.setLastName(lName1);
                student4.setMajor(major1);
                student4.setGpa(gpa1);
                names.add(student4);
                break;
            case 2:

                System.out.print("Find student with sNumber S");
                input.nextInt();
                student1.getsNumber();
                //statement on finding a student
                break;
            case 3:
                //statement on deleting a student
                break;
            case 4:
                System.out.println(student1);
                System.out.println(student2);
                System.out.println(student3);
                break;
            case 5:
                names.size();
                break;
            case 6:
                System.out.println("Good bye");
                break;
            default:
                System.out.println("Invalid Selection");
                System.out.println();
                break;
            }
            System.out.println();
        }while(choice != 0 );


    }

    public static void displayMenu()    {
        System.out.println("1. Add a student");
        System.out.println("2. Find a student");
        System.out.println("3. Delete a student");
        System.out.println("4. Display all students");
        System.out.println("5. Display the total number of students");
        System.out.println("6. Exit");
    }

}

Add static attribute (studentNumberSequence) to the Student class with initialize value (eg 1234567) and increase it's value in constructors. 使用初始化值(例如1234567)将静态属性(studentNumberSequence)添加到Student类,并在构造函数中增加其值。 You can put the following line in constructors: 您可以将以下行放入构造函数中:

sNumber = studentNumberSequence++;

I suggest you to use Map (eg HashMap) instead of ArrayList and apply 'sNumber' as the map key. 我建议您使用Map(例如HashMap)代替ArrayList并应用“ sNumber”作为地图键。

There's no reason to try to do this the way you are. 没有理由按照您的方式尝试执行此操作。 Consider that you can simply define an initial student number; 考虑到您可以简单地定义一个初始学生编号; according to your code I would assume this would be int studentNumber = 1234567; 根据您的代码,我认为这将是int studentNumber = 1234567; .

Now, when you add a student to the list don't neglect to add that number or initialize all students with the same number; 现在,当您将一个学生添加到列表中时,不要忽略添加该数字或用相同的数字初始化所有学生; that's just going to over complicate things. 那只会使事情复杂化。

Given that you now have a number, get the other information from the user and add a new Student with those values plus the student number that you assign for them. 假设您现在有了一个号码,请从用户那里获取其他信息,然后添加一个新的Student ,其中包含这些值以及您为其分配的学生编号。 For example: 例如:

names.add(new Student(firstName, lastName, studentNumber++, major, gpa));

Additionally, you have a number of other issues that I won't address here but warn you about. 此外,您还有许多其他问题,在这里我不会解决,但会警告您。

  1. The count should not come from a student instance. 该计数不应来自学生实例。
  2. Getting the student number should just give the student number (now that you have that figured out) 获取学生编号应该只给出学生编号(现在您已经知道了)
  3. Take care when you're naming constructor parameters the same as the instance variables; 将构造函数参数命名为与实例变量相同时,请务必小心; you need to distinguish between them somehow. 您需要以某种方式区分它们。
  4. You can't hardcode the outputting of all students; 您不能对所有学生的输出进行硬编码。 they can add more. 他们可以添加更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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