如何编辑和删除列表

时间:2016-09-21 12:20:32

标签: java

这是我的任务:我正在制作一个简单的记录保存程序"公共汽车信息系统"。我已经编写了一些代码,但我不知道如何删除和编辑Java中的列表。有人可以教我如何,或者给我一些示例代码吗?

以下是我的代码:

Bus.java

   public class Bus
    {   
       private String Dname;
       private int  PlateNo;
       private int      Capa;
       private String   Atime;
       private String   Dtime;
       private String   Route;
       private String   Type;

         private static int busCount;

          public Bus(){
          Dname = "";
          Atime = "";
          Dtime = "";
          Capa = 0;
          PlateNo = 0;
          Route = "";
          Type = "";
          busCount++;
                      }

     public Bus(int PlateNo, String Atime, String Dtime, String Route){
         this.PlateNo = PlateNo;
         this.Atime = Atime;
         this.Dtime = Dtime;
         this.Route = Route;
         busCount++;
}


public void setDNAME( String Dname ){
          this.Dname = Dname;
}

public void setATIME( String Atime ){
          this.Atime = Atime; 
}

public void setDTIME( String Dtime ){
          this.Dtime = Dtime;

}

public void setCapa( int Capa ){
          this.Capa = Capa; 
}

public void setPLATENO( int PlateNo ){
          this.PlateNo = PlateNo;   
}

public void setROUTE( String Route ){
          this.Route = Route;   
} 

public void setTYPE( String Type ){
          this.Type = Type; 
}


public String getDNAME(){
    return Dname;
}
public String getATIME(){
    return Atime; 
}
public String getDTIME(){
    return Dtime;
}
public int getCAPA(){
    return Capa;    
}
public int getPLATENO(){
    return PlateNo; 
}
public String getROUTE(){
    return Route;   
}
public String getTYPE(){
    return Type;    
}

public static int getBusCount(){
    return busCount;
}


public void print(String DN, String PN, int C, String AT, String DT, String R, String T){
    System.out.printf("%-10s : %s\n", "Driver's Name", DN);
    System.out.printf("%d :\n", "Plate No", PN);
    System.out.printf("%d :\n", "No. of Set Capacity", C);
    System.out.printf("%-10s : %s\n", "Arrival Time", AT);
    System.out.printf("%-10s : %s\n", "Departure Time", DT);
    System.out.printf("%-10s : %s\n", "Route", R);
    System.out.printf("%-10s : %s\n", "Type", T);
}

}

BusMain.java

import java.util.Scanner;

public class BusMain{
public static void main(String[] args){     

    int x=0;
    int menuChoice=-1;

    Bus[] bus=new Bus[100];

    Scanner input = new Scanner(System.in);

    do{
        System.out.println("============ BUS INSTALLATION ==============");
        System.out.println("Bus Information System");
        System.out.println("1.Add New Bus\n2. View Buses\n3. Search for a Bus\n4. Exit");
        System.out.printf("Your choice: ");

        menuChoice = input.nextInt();
        Bus s = new Bus();
        if(menuChoice==1){

            cls;

            if(x<100){
                System.out.printf("BUS Plate No.: ");
                s.setPLATENO(input.nextInt());

                System.out.printf("Arrival Time: ");

                s.setATIME(input.nextLine());

                System.out.printf("Departure Time: ");
                s.setDTIME(input.nextLine());

                System.out.printf("Destination: ");
                s.setROUTE(input.nextLine());

                bus[x] = s;

                x++;                                    
            }else{
                System.out.println("Can't add new bus, Terminal full");
                }
            }
            else if (menuChoice==2){
            for (int i=0; i<x; i++){
                s = bus[i];
                System.out.printf("PLATE NO: %d\n", s.getPLATENO());    
                System.out.printf("ARRIVAL TIME: %s\n",s.getATIME());   
                System.out.printf("DEPARTURE: %s\n",s.getDTIME());  
                System.out.printf("DESTINATION: %s\n",s.getROUTE());    
            }

        }
        else if(menuChoice<1 || menuChoice>4){
            System.out.println("Not in the Menu ;please re-enter");
        }
    } while (menuChoice != 4);

     input.close();
   }
 }

1 个答案:

答案 0 :(得分:-1)

根据您的上一条评论。您可以将所有数组对象放入列表中:           Arrays.asList(arrObject);

然后,您可以浏览Java文档,了解如何操作List进行编辑和删除。