English 中文(简体)
Java 矩阵: 将对象添加到数组, 但无效。 如何修改?
原标题:Java Arrays: Added object to array but is null. How to modify?
  • 时间:2012-05-23 04:06:14
  •  标签:
  • java
  • arrays

因为这个我经营着蓝J 做着以物体为导向的雅瓦 我让JVM 7.1活动...

谢天谢地"老师"让我使用我的三组系统 因为循环器毁了我的单组系统

我无法在互联网上找到任何帮助我解决问题的东西。

基本上当我去向数组添加“ 学生” 时, 该对象在所有字段中都是空的。 我无法找到如何修改它们 。

我的阵列使用钻石标记。 类似 :

private ArrayList<Student> students;

这是目前我使用的数组形式, 还有两个数组 。

private ArrayList<CollegeEmployee> staff;
private ArrayList<Faculty> members;

有人建议我用一个阵列,但正如所说, 传动器搞砸了它 只要我能修好它, 我不在乎我有多少阵列。

程序在一时段循环运行, 并提示用户输入特定命令的字母。 当用户输入“ s” 时, 将学生添加到数组中, 问题就出现在这里。 对象被给定为无效值, 我并不知道如何修改这些值... 以下是调用对象构造器的方法 。

private void addStudent()
{
    Student b = new Student(firstName, lastName, address, postCode, phoneNumber, gradePointAvg, selectedCourse);
students.add(b);
}

当当当当用户退出程序时打印数组时, 我从终端窗口获得以下输入 :

null, null, null, 0, 0 0 null

我的问题是: 我如何给对象 b 值来取代空号 。 数值必须由用户输入, 而不是在数组声明中预设 。

感谢您抽出时间:

这东西让我憎恨代码XD

编辑:

以下是学生班的建筑师和田地:

    private String title;
        public double gradePointAvg;
        public String selectedCourse;

        /**
         * Constructor for objects of class Student
         */
        public Student(String firstName, String lastName, String address, int postCode, int phoneNumber, double gradePointAvg, String selectedCourse)
        {
            super(firstName, lastName, address, postCode, phoneNumber);
            this.title = title;
            this.gradePointAvg = gradePointAvg;
            this.selectedCourse = selectedCourse;
        }

个人超级舱位建造者:

public String firstName;
public String lastName;
public String address;
public int postCode;
public int phoneNumber;

 /**
 * Constructor for objects of class Person
 */
public Person(String firstName, String lastName, String address, int postCode, int phoneNumber)
{
    // initialise fields
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.postCode = postCode;
    this.phoneNumber = phoneNumber;
}

上面的方法,加上学生叫学生建筑师,然后叫人建筑师。是的,它必须像这样作为项目的限制。

我暂时把田地公诸于世 因为它们是受保护的 因为私人是地方范围

感谢你们检查我的代码。 我希望我的老师好些了 & gt;. & lt;

最佳回答

在您的学生类 firstName, lastName, 地址, 选择的 可能是 string , Java将初始化属于对象的例变量(在您的情况中)为 null , 对于您的其他字段, 即 postCode, 电话编号 , 我认为是 > 默认值为 0 。

当您在 adds Student 方法中检查用于构建对象的值时,似乎所有字符串都是空的,未初始化。

Student b = new Student(firstName, lastName, address, postCode, phoneNumber, gradePointAvg, selectedCourse);

当上面的代码被执行时, 我认为没有一个参数像第一个Name, 最后一个Name e.t.c 有任何价值, 这就是为什么它显示您为无效 。

在您添加新的 student 对象到您的列表 students 之前,请提醒您的用户给 firstName, lastName, address e.t.c, 然后用这些数值构建您的 student 对象 。

更新样本代码 :

学生班:

package com.mumz.test.scanner;

public class Student {
    private String firstName = null;
    private String lastName = null;
    /**
     * @param firstName
     * @param lastName
     */
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }
    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }
    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("Student [firstName=%s, lastName=%s]", firstName, lastName);
    }
}

主班级

package com.mumz.test.scanner;

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

public class StudentScannerApp {
    private List<Student> students = new ArrayList<Student>();
    public static void main(String[] args) {

        StudentScannerApp studentScannerApp = new StudentScannerApp();
        //This scanner we will use for reading data from input stream
        Scanner inputStream = new Scanner(System.in);

        System.out.println("Enter First Name");
        String firstName = inputStream.nextLine();
        System.out.println("Enter Last Name");
        String lastName = inputStream.nextLine();
        studentScannerApp.addStudent(firstName, lastName);

        System.out.println("Enter Another student details");
        System.out.println("Enter First Name");
        firstName = inputStream.nextLine();
        System.out.println("Enter Last Name");
        lastName = inputStream.nextLine();
        studentScannerApp.addStudent(firstName, lastName);

        System.out.println(studentScannerApp.students);
    }

    private void addStudent(String firstName, String lastName){
        Student student = new Student(firstName, lastName);
        students.add(student);
    }
}
问题回答

你初始化了阵列列表吗?

 private ArrayList<Student> students;
 students = new ArrayList<Student>();

其它

private ArrayList<Student> students = new ArrayList<Student>();

如果不初始化的话, 将返回无效值, 您将获得无效指针例外 。

我想是学生的拉长,不是吗?

那你的建筑师应该长成这个样子

public Student(String firstName, String lastName, String address, int postCode, int phoneNumber){
    super(firstName, lastName, address, postCode, phoneNumber);
    //Do whatever stuff Students do and normal Persons do not do.
}

学生的方法应该这样增加学生:

private void addStudent(Student student){
    students.add(student);
}

(无论如何使 私人并不真正 说得通,但我想你还在学习)

所以,当你想创建和添加新学生时,你只要这样做(当然,你可以这样做,但如果你不想在阵列中增加学生数量,就应该使用变量)

Student student = new Student("John", "Smith", "Fake Street", 123, 12345, 5555555);
students.add(student);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签