我已经想出如何从用户那里收集数据以在我的阵列中创建一个新学生,但是我在将这些信息添加到数组时遇到了麻烦。请告诉我如何在给定代码中将此数据添加为新的Student对象。请参考AddStudent
方法。
import java.util.Scanner;
public class ArrayDemo {
static Student[] students;
private static void ViewStudents() {
for (int i = 0; i < students.length; i++) {
System.out.println(i + ") " + students[i].getLName() + ", " + students[i].getFName());
}
}
private static void ViewDetails() {
Scanner kb = new Scanner(System.in);
int i;
System.out.println("Who would you like to view?");
ViewStudents();
i = Integer.parseInt(kb.nextLine());
System.out.println("ANum:\t\t" + students[i].getANum());
System.out.println("\nAddress:\t" + students[i].address.getHouseNum() + " " + students[i].address.getStreet());
System.out.println("\t\t" + students[i].address.getCity() + ", " + students[i].address.getState() + " " + students[i].address.getZip());
System.out.println("\t\t" + students[i].address.getLine2());
}
private static void AddStudent() {
Scanner kb = new Scanner(System.in);
Student student = new Student();
String FirstName;
String LastName;
int HouseNum;
String Street;
String City;
String State;
int Zip;
String Line2;
System.out.println("\tInput Information");
System.out.println("\tFirst Name:");
FirstName = kb.nextLine();
System.out.println("\tLast Name:");
LastName = kb.nextLine();
System.out.println("\tHouse Number:");
HouseNum = Integer.parseInt(kb.nextLine());
System.out.println("\tStreet:");
Street = kb.nextLine();
System.out.println("\tCity:");
City = kb.nextLine();
System.out.println("\tState:");
State = kb.nextLine();
System.out.println("\tZip Code:");
Zip = Integer.parseInt(kb.nextLine());
System.out.println("\tExtra Information:");
Line2 = kb.nextLine();
System.out.println("\nStudent:\t" + LastName + ", " + FirstName);
System.out.println("ANum:\t\t" + student.getANum());
System.out.println("Address:\t" + HouseNum + " " + Street);
System.out.println("\t\t" + City + ", " + State + " " + Zip);
System.out.println("\t\t" + Line2);
//students.setAddress( HouseNum, Street, City, State, Zip, Line2 );
System.out.println("\tYour Student was Successfully Added");
}
private static void RemoveStudent() {
Scanner kb = new Scanner(System.in);
System.out.println("Who would you like to remove?");
ViewStudents();
for (int j = Integer.parseInt(kb.nextLine()); j < students.length - 1; j++) {
students[j] = students[j + 1];
}
students[students.length - 1] = null;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int x = 40;
//students = new Student[0];
students = new Student[2];
students[0] = new Student("Thomas", "Emily");
students[1] = new Student("Bob", "Joe");
students[0].address = new Address(6614, "White Sands ln", "Hixson", "Tennessee", 37343, "");
students[1].address = new Address(66, "White ln", "Hson", "Tealamabaee", 373873, "");
do {
System.out.println();
System.out.println("Do you want to:");
System.out.println("\t0) View Students");
System.out.println("\t1) View Students' Details");
System.out.println("\t2) Add a Student");
System.out.println("\t3) Remove a Student");
System.out.println("\t4) Exit");
x = Integer.parseInt(kb.nextLine());
switch (x) {
case 0:
ViewStudents();
break;
case 1:
ViewDetails();
break;
case 2:
AddStudent();
break;
case 3:
RemoveStudent();
break;
case 4:
break;
default:
}
} while (x != 4);
}
}
答案 0 :(得分:1)
请相信我,如果不知道阵列中当前有多少条记录,就无法向阵列添加记录。如果您不想要另一个静态变量来跟踪记录数,则必须循环遍历数组,直到它为空。
static int numOfStudents = 0; //declare outside your main
public static void AddStudent()
{
Scanner scn = new Scanner( System.in );
if (numOfStudents < students.length){
System.out.println("Enter last name:");
String ln = scn.nextLine():
System.out.println("Enter first name:");
String fn = scn.nextLine():
Student stud = new Student(ln, fn);
students[numOfStudents] = stud;
numOfStudents ++;
}
}
您更喜欢的替代解决方案。但是在我看来这很糟糕。
public static void AddStudent()
{
Scanner scn = new Scanner( System.in );
int index=0;
while(x<students.length && student[x] != null)
index++; //get position to add new student
System.out.println("Enter last name:");
String ln = scn.nextLine():
System.out.println("Enter first name:");
String fn = scn.nextLine():
Student stud = new Student(ln, fn);
students[index] = stud;
}
答案 1 :(得分:1)
从我所看到的,你使用的是错误的数据结构。您似乎要添加动态数量的Student
s,而ArrayList
会比Array
更好,更合适。然后,您只需使用add
方法。
请记住,Array
的大小是不可变的(它不能更改)所以在这种情况下你想要的肯定是某种List
,可能是ArrayList
如果您坚持使用数组,则需要跟踪学生已添加的数字并将其用作索引(作为全局变量或将其作为参数传递)。但请记住,Array
的性质意味着除非你设置了非常高的容量,否则你将很快达到上限,
答案 2 :(得分:0)
对代码进行大量更改的最简单方法是在students数组旁边添加一个名为index的类变量。此变量将跟踪自arrays in Java have fixed length以来的学生数量:
public class ArrayDemo
{
static Student[] students;
static int index = 0; // keeps track of the amount of students in the students array
// ... rest of your code
然后在AddStudent()
内,您将从输入中获得的所有信息添加到索引处的当前学生并更新索引:
private static void AddStudent() {
// ... the code where you obtain user information
if(index < students.length) { // make sure there is room to add the user
Student student = new Student(/* add user information into the constructor */);
students[index] = student; // add the user at the index
index++; // update the index
} else {
System.err.println("No more room for students");
}
}
上面的代码假设您有一个接受必要用户信息的构造函数。
否则您可以单独分配信息:
Student student = new Student();
student.firstName = firstName;
// ...
或者使用像:
这样的setterstudent.setFirstName(firstName);
如果您不使用索引来跟踪用户数量并使用更加动态的方式存储学生,那么我建议使用List
或某些扩展它的类,例如{ {3}}
答案 3 :(得分:0)
您的数组具有2个元素的固定大小。您可以改为使用更大的MAX值(正如其他答案所暗示的那样)保留一个变量来计算填充的数组索引的实际数量。
static int ARRAY_MAX = 10;
static Student[] students;
static int numStudents;
...
students = new Student[ARRAY_MAX];
numStudents = 0;
添加学生会看起来像......
students[numStudents] = new Student();
numStudents++;
...但是当numStudents大于等于10(ArrayIndexOutOfBoundsException)时它会中断。
在这种情况下,您需要放大(调整大小)阵列。在Java数组中,数组具有固定的大小 - 调整数组大小相当于声明一个全新的数组(增加大小),然后将旧数组复制到新数组中。使用System.arraycopy将内容从一个数组复制到另一个数组中。
你应该只使用一个ArrayList - 在幕后这是该对象正在做的事情。