首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除学生数组中的“学生”

删除学生数组中的“学生”
EN

Stack Overflow用户
提问于 2014-12-11 02:01:46
回答 3查看 5.1K关注 0票数 0

我想不出如何删除学生数组中的一个学生。我需要继续数组,没有空白或中断,我有一些困难,这样做。在添加学生时,我也遇到了将信息设置到数组中的问题。我可以要求信息,但保存到数组,我无法弄清楚。

代码语言:javascript
复制
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

进口java.util.Random;

公共班级学生{

代码语言:javascript
复制
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 );
}

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-11 02:10:57

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

代码语言:javascript
复制
ArrayList<Student> = new ArrayList();

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

或者,把所有的东西都移开可以达到这个目的。

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

代码语言:javascript
复制
for(int i = 3; i < arr.length - 1; i++)
    arr[i] = arr[i + 1];
arr[arr.length - 1] = null;

若要添加对象(仍然是foo类型的数组),

代码语言:javascript
复制
for(int i = 0; i < arr.length; i++) {
    if(arr[i] == null) { 
        arr[i] = bar;
        break;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2014-12-11 02:41:29

当我看到student这个词时,我觉得这是一项学校的工作。因此,我不会要求您使用数组以外的任何东西。

删除数组中的记录:

在数组中,如果要删除没有中间空白的记录,则必须逐个“移动”记录。这是缩小数组中空白的唯一方法。(不使用任何其他数据结构)。

代码语言:javascript
复制
//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. 如果还有更多的空间,请在最后一个可用的插槽处添加。

添加学生记录:

代码语言:javascript
复制
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);
}

我见过许多大学/学院都有这样的作业。他们通常希望你跟踪你目前拥有的记录的数量。

因为你没有发布你的学生班级的样子。如果在“类”中有一个静态变量,记录所添加的对象的数量。您不必手动跟踪记录的数量。如果你在学生课堂上有一个柜台,它会是这样的:

代码语言:javascript
复制
public class Student
{
    static int numOfRecords = 0;
    public Student()
    {
        numOfRecords++;        
    }      
}

若要维护当前的状态,请在主服务器外再添加一个静态变量。(看起来您不想将任何东西传递给方法)

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-12-11 03:21:25

代码语言:javascript
复制
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重新分配了内存。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27414176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档