【发布时间】:2019-06-13 20:50:39
【问题描述】:
我是 java 新手,但一直在玩数组列表,现在卡住了。
我从一个名为 Car 的类创建了一个数组列表,其中包含三个参数,其中一个称为 timesmoved。
主类
public class GarageTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// create Bank object
Garage bashimGarage = new Garage() ;
// create Scanner object associated with input file
Scanner fileScan = new Scanner(new
File("C:\\Users\\jamison\\Desktop\\GarageData.txt")) ;
// read BankAccount data from file, create objects, and add to list
while ( fileScan.hasNextLine()) // while not eof
{
String fullText = fileScan.nextLine();
// Split the acquired string into 2 based on the whitespace
String[] splitText = fullText.split("\\s+");
// String before whitespace split
String licensePlate = splitText[0];
// String after whitespace split
String status = splitText[1];
// create Car object
Car newCar = new Car(licensePlate, status , 0) ;
// add to list
bashimGarage.addCar( newCar ) ;
}
/*
*Calculates the number of times car was temporary moved before departure
*/
bashimGarage.carDepart();
/*
*Prints list of car license plates
* Admits or declines a car to the garage
* Prints if a car departs the Garage
* When a car departs also prints the number of times it was moved
*/
bashimGarage.moveCarInGarage();
汽车类
public class Car {
private String licensePlate; // License Plate Number
private String status ; // Status: Arivved or Departed
private int moved; /* How many times the car
got moved out of the garage
*/
public Car ( String licenseNum, String carStatus , int timesMoved)
{
licensePlate = licenseNum ;
status = carStatus ;
moved = timesMoved;
}
public String getLicenseNum()
{
return licensePlate;
}
public String getStatus()
{
return status;
}
public int getTimesMoved()
{
return moved;
}
public int setTimesMoved(int times){
moved = moved + times;
return moved;
}
}
车库类
public class Garage {
private ArrayList<Car> list ; // a list of BankAccount objects
public int maxCars = 10; // max number of cars allowed in garage
public int currentCars = 0; // current number of cars in garage
public Garage()
{
list = new ArrayList<Car>() ;
}
public void addCar(Car newCar)
{
list.add(newCar) ; // calls "add" method of ArrayList class
}
public void carDepart() {
for (int i = 0; i < list.size(); i++) {
Car current = list.get(i); // get next car
if (current.getStatus().equals("DEPART")) {
int pos = list.indexOf(current);
for (int j = 0; j < pos; j++) {
list.get(j).setTimesMoved(1 + current.getTimesMoved());
}
list.remove(i);
return;
}
}
}
public void moveCarInGarage()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("ARRIVE"))
{
currentCars++; //Increments current # of cars in garage
if (currentCars <= 10) // Checks if there is space in garage
{
//Prints license plate and arrival status to screen
System.out.println("Car with license plate" +
current.getLicenseNum() + " has arrived "
+ "and been moved into the garage");
}
else
{
// Prints garage is full to screen
System.out.println("The garage is full at this " +
"time so come back later");
}
}
else
{
currentCars--; // Decrements current # of cars in garage
/*
Prints license plate, departure status,
and number of times moved to screen
*/
System.out.println("Car with license plate" +
current.getLicenseNum() + " is departing and has been moved "
+ current.getTimesMoved() + " times" );
}
}
}
我正在读取应该像车库一样的文件的输入,并说明汽车是“到达”还是“出发”
我正在尝试使用 if 语句编写代码,如果状态为“Departing”,则当前元素将被删除,它前面的所有元素都会在其 “timesmoved parameter”中添加一个强>
我坚持的部分是,根据被删除的元素,数组列表中它前面的所有元素都将其 "timesmoved" 参数加一。
我想出了这个,但它似乎不起作用,因为当我调用第二种方法时,它总是说 0 表示移动的时间。
public void carDepart()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("DEPART"))
{
int pos = list.indexOf(i);
for ( int j = 0 ; pos < j ; j++)
{
current.setTimesMoved(1 + current.getTimesMoved());
}
list.remove(i);
return;
}
}
}
第二种方法
public void moveCarInGarage()
{
for ( int i = 0 ; i < list.size() ; i++ )
{
Car current = list.get( i ) ; // get next car
if (current.getStatus().equals("ARRIVE"))
{
currentCars++;
if (currentCars <= 10)
{
System.out.println("Car with license plate" +
current.getLicenseNum() + " has been moved into the garage");
}
else
{
System.out.println("The garage is full at this " +
"time so come back later");
}
}
else
{
currentCars--;
System.out.println("Car with license plate" +
current.getLicenseNum() + " is departing and has been moved "
+ current.getTimesMoved() + " times" );
}
}
}
【问题讨论】:
标签: java for-loop if-statement arraylist