创建实例并将其存储在阵列 [英] create instances and store them in array
本文介绍了创建实例并将其存储在阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个学生类,并创建需要被存储在array.attributes可在程序给予或input.how我延长我的main()方法来存储它的用户阅读每一个学生的实例?我坚持。
下面是我的学生课code:
公共类学生{ INT studentID;
串studentName,性别,当然;
/ **设置学生姓名
* @参数studentName
* /
公共无效setname可以(字符串studentName){
this.studentName = studentName;
}
/ **
*组学号
* @参数studentID
* /
公共无效setNewId(INT studentID){
this.studentID = studentID;
}
/ **
*设置学生性别
* @参数性别
* /
公共无效setGender(字符串性别){
this.gender =性别;
}
/ **
*设置课程
* @参数当然
* /
公共无效setCourse(字符串课程){
this.course =课程;
}
/ **
*获取学生的ID号。
* @返回的ID号学号。
* /
公众诠释getIdNumber(){
返回studentID;
} / **
*获取学生的名字。
* @返回studentName学生姓名。
* /
公共字符串的getName(){
返回studentName;
}
/ **
*获取学生的性别
* @返回
* /
公共字符串getGender(){
返回性别;
}
/ **
*获取学生的课程。
* @回报当然学生课程。
* / 公共字符串getCourse(){
返回课程;
}
/ **
*打印的学生信息。
* @返回学号,姓名,性别,当然。
* / 公共无效printStudent(){
System.out.print(studentID ++ studentName ++性别++课程);
}
}
这是我的主类main()方法需要数组:
公共类SMSMain {
/ **
*
* @参数ARGS
* /
公共静态无效的主要(字串[] args){
//创建学生对象的实例 学生A =新的学生();
a.studentName =玛丽亚;
a.studentID = 1236;
的System.out.println(学生姓名+ a.studentName);
的System.out.println(学号+ a.studentID);
}
}
解决方案
您需要启动阵列和循环中创建对象。
学生[]学生;
的System.out.println(输入学生人数:);
DataInputStream以在=新DataInputStream所(System.in);
INT N = in.readInt();
学生=新的学生[N];
的for(int i = 0; I< N;我++){
学生[i] =新学生();
的System.out.println(姓名:);
学生[I] .setName(in.readLine());
的System.out.println(ID);
学生[I] .setNewId(in.readInt());
}
I have a student class and every student instance created needs to be stored in an array.attributes can be given in program or read from user input.how do I extend my main() method to store it? i am stuck .
Here is my student class code:
public class student {
int studentID;
String studentName,gender,course;
/** set the student name
* @param studentName
*/
public void setName(String studentName){
this.studentName = studentName;
}
/**
* set student ID number
* @param studentID
*/
public void setNewId(int studentID){
this.studentID = studentID;
}
/**
* Set student gender
* @param gender
*/
public void setGender(String gender){
this.gender = gender;
}
/**
* set the course
* @param course
*/
public void setCourse(String course){
this.course = course;
}
/**
*Gets the Student's ID Number.
*@return IdNumber Student ID Number.
*/
public int getIdNumber(){
return studentID;
}
/**
*Gets the Student's Name.
*@return studentName Student Name.
*/
public String getName(){
return studentName;
}
/**
* Get student gender
* @return
*/
public String getGender(){
return gender;
}
/**
*Gets the Student's Course.
*@return course Student Course.
*/
public String getCourse(){
return course;
}
/**
*Prints Student Informations.
*@return Student ID Number, name, gender and course.
*/
public void printStudent(){
System.out.print(studentID+""+studentName+""+gender+""+course);
}
}
And here is my main class with main() method that needs the array:
public class SMSMain {
/**
*
* @param args
*/
public static void main(String[] args) {
// Create an instance of student object
student a = new student();
a.studentName = "Maria";
a.studentID = 1236;
System.out.println("Student Name:" + a.studentName);
System.out.println("Student ID:" + a.studentID);
}
}
解决方案
You need to initiate the array and create objects within loop.
Student[] students;
System.out.println("Enter number of students :");
DataInputStream in = new DataInputStream(System.in);
int n = in.readInt();
students = new Student[n];
for(int i=0; i<n ;i++) {
students[i] = new Student();
System.out.println("Name:");
students[i].setName(in.readLine());
System.out.println("Id:");
students[i].setNewId(in.readInt());
}
这篇关于创建实例并将其存储在阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文