删除学生数组中的学生

时间:2014-12-11 02:01:47

标签: java arrays

我无法弄清楚如何删除学生数组中的学生。我需要继续阵列没有间隙或休息,我在做这件事时遇到了一些麻烦。在添加学生时,我也在将信息设置到数组中时遇到问题。我可以要求提供信息,但将其保存到我无法弄清楚的阵列中。

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( "\tFirst:" + student.getFName() + "\n\tLast:" + student.getLName() + "\n\tA-Number:" +student.getANum()); */

    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 );
    int i;
    System.out.println( "Who would you like to remove?");ViewStudents();
    i = Integer.parseInt( kb.nextLine() );

    for( i < student.length - 1; i++)
    { students[i] = students[i + 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);

}

}

Student.java

import java.util.Random;

公共班学生 {

Address address; //javac will now compile Address.java

// List private data first -- it's polite to my programmer-user.
private String LName;   // Last Name
private String FName;   // First Name
private int ANum;       // A number

public Student()
{
    Random rand = new Random();

    LName = "";
    FName = "";
    // ANum  =  0;
    ANum = rand.nextInt( 99999999 );
}

public Student( String ln, String fn/*, int an*/ )
{
    Random rand = new Random();

    LName = ln;
    FName = fn;
    // ANum  = an;
    ANum = rand.nextInt( 99999999 );
}

public boolean setLName( String ln )
{
    LName = ln;
    return true;
}

public String getLName()
{
    return LName;
}

public boolean setFName( String fn )
{
    FName = fn;
    return true;
}

public String getFName()
{
    return FName;
}

// public boolean setANum( int an )
// {
    // ANum = an;
    // return true;
// }

public String getANum()
{
    // String str = String.format( "A%08d", ANum );
    // return "A" + ANum;
    // return str;
    return String.format( "A%08d", ANum );
}

}

3 个答案:

答案 0 :(得分:1)

当我看到单词student时,我觉得这是一项学校作业。因此,我不会要求你使用除阵列以外的任何东西。

删除数组中的记录:

在数组中,如果你想要删除中间没有间隙的记录,你必须&#34;向上移动&#34;记录背后的记录。这是填补数组间隙的唯一方法。 (不使用任何其他数据结构)。

//record of student, index to be deleted, number of records you have
public static int deleteRecord(Student[] record, int idx, int numOfRecords)   
{
    if(idx < 0 || idx > numOfRecords) //Check index is valid
        return -1;

    for(int x=idx; x<numOfRecords; x++) //closing the gap by copying the next value
        record[x] = record[x+1];

    return (--numOfRecords);
}

要在数组中添加记录:

  1. 检查是否已达到记录数限制。
  2. 如果还有空间,请添加最后可用广告位。
  3. 添加学生记录:

    public static int addRecord(Student[] record, int numOfRecords)   
    {
        if(numOfRecords >= record.legnth) //Check record is not full yet
            return -1;
    
        //prompt for student particulars
        record[numOfRecords].name = xxx;   //where xxx is input by user
        record[numOfRecords].id = yyy;   //where yyy is input by user
        return (++numOfRecords);
    }
    

    我见过很多大学/大学都有此类任务。他们通常希望您跟踪您当前拥有的记录数量。

    因为您没有发布您的学生课程的样子。如果您在Class Student中有一个静态变量,则记录添加的学生对象数。您不必手动跟踪记录数量。如果您的学生班中有一个计数器,它将会是这样的:

    public class Student
    {
        static int numOfRecords = 0;
        public Student()
        {
            numOfRecords++;        
        }      
    }
    

    要维护您当前拥有的内容,请在主外添加一个静态变量。 (看起来你不想把任何东西传递给方法)

    static int numOfRecords = 0; //declare outside your main
    
    public static void AddStudent()
    {
        Scanner scn = new Scanner( System.in );
    
        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[numOfRecords] = stud;
        numOfRecords ++;
    }
    

    您需要添加的所有内容。

答案 1 :(得分:0)

ArrayList<E>是你的朋友。这样,您可以添加和删除元素,并自动填充空白。需要import java.util.ArrayList<E>;

ArrayList<Student> = new ArrayList();

确保大小为10,可以使用size()方法访问。 add(E e)允许您附加到列表中,remove(Object o)remove(int i)可以删除特定索引或E类型的特定实例。

或者,将所有东西都移开可以解决问题

给定类型为Foo的数组,假设您要删除索引3处的对象。

for(int i = 3; i < arr.length - 1; i++)
    arr[i] = arr[i + 1];
arr[arr.length - 1] = null;

要添加对象(仍为foo类型的数组),

for(int i = 0; i < arr.length; i++) {
    if(arr[i] == null) { 
        arr[i] = bar;
        break;
    }
}

答案 2 :(得分:0)

import java.util.Arrays;

public class Test
{
    static Student[] students = new Student[3];

    public static void main (String[] args) {
        students[0] = new Student ("Thomas");
        students[1] = new Student ("Bob");
        students[2] = new Student ("Mark");

        removeStudent(1);

        for (Student s : students) {
            System.out.println(s.getName());
        }

    }

    public static void removeStudent (int index) {
        // valid index
        if (index < 0 || index > students.length) {
            return;
        }

        // null it
        students[index] = null;

        // move all elements after index back
        for (int i = index; i < students.length-1; i++) {
            students[index] = students[index+1];
        }

        Student[] temp = Arrays.copyOf(students, students.length-1);
        students = temp;
    }
}

这是您的代码的简化版本。学生现在只有一个带吸气剂的名字。但是removeStudent重新分配students的内存。

希望你能从中汲取一些智慧。