【发布时间】:2017-01-29 16:42:04
【问题描述】:
这是我的任务:我正在制作一个简单的记录保存程序“公交信息系统”。我已经写了一些代码,但是我不知道如何在 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();
}
}
【问题讨论】:
-
对不起....已经没有标签了
-
如果您要求人们尝试阅读代码,您应该正确缩进代码。
-
不清楚你在问什么。您的问题使用数组而不是列表。
-
对不起,我的想法是如何删除和编辑存储在数组中的对象列表..??
-
@Spintacle - 除非您被明确指示使用数组,否则您应该阅读
List+ArrayList和Map+HashMap的 javadocs 并使用其中一种抽象.
标签: java