删除的学生在学生阵列 [英] Deleting a Student in a student array

查看:102
本文介绍了删除的学生在学生阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何删除我的学生阵列的学生。我需要继续无缺口或断裂的阵列和我有一些麻烦这样做。我也有问题设置的信息放入数组增加一个学生的时候。我可以要求信息,但它保存到数组我无法弄清楚。

I cannot figure out how to delete a student in my student array. I need to continue the array with no gaps or breaks and i am having some trouble doing that. I am also having issues setting the information into the array when adding a student. I can ask for the information but saving it to the array I cant figure out.

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

Student.java

导入了java.util.Random;

import java.util.Random;

公共类学生
{

public class Student {

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 );
}

}

推荐答案

的ArrayList&LT; E&GT; 这里是你的朋友。有了这个,你可以添加和删除元素,配合间隙自动灌装。需要进口的java.util.ArrayList&LT; E&GT ;;

ArrayList<E> is your friend here. With this, you can add and remove elements, with the gaps filling up automatically. Need import java.util.ArrayList<E>;

ArrayList<Student> = new ArrayList();

确保大小为10,它可与尺寸()方法来访问。 添加(E E)将允许您添加到列表中,而删除(对象o)删除(int i)以让你删除一个特定指数或E型的具体实例。

Ensures a size of 10, which can be accessed with the size() method. add(E e) will allow you to append to the list, and remove(Object o) or remove(int i) lets you remove either a specific index or a specific instance of type E.

另外,转移了所有一切可以做的伎俩

Alternatively, shifting everything away could do the trick

给定类型为,说你想在指数3去除对象。数组

Given an array of type Foo, say you want to remove the object at index 3.

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

要添加对象(仍然Foo类型的数组),

To add an object(still an array of type foo),

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

这篇关于删除的学生在学生阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆