猿问

根据之前的 ELEMENTS 递增 ArrayElement 参数

我是 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;

}


蓝山帝景
浏览 111回答 2
2回答

绝地无双

第一种方法,第 8 行。为什么要pos在i. int pos = list.indexOf(i);会给-1。另外,在这个循环中for ( int j = 0 ; pos < j ; j++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current.setTimesMoved(1 + current.getTimesMoved());&nbsp; &nbsp; }你总是指向数组列表中的同一个元素。相反,您可能希望像下面这样对其进行编程:for ( int j = 0; j<i; j++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Car car = list.get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; car.setTimesMoved(1 + car.getTimesMoved());&nbsp; &nbsp; }

茅侃侃

您的方法中有以下问题,首先你得到一个整数而不是对象的索引。您的内部 for 循环条件是错误的。您总是增加当前对象的值而不是以前的值。我已经用你的方法纠正了它们。使用以下一种,public void carDepart() {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Car current = list.get(i);&nbsp; &nbsp;// get next car&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (current.getStatus().equals("DEPART")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* You can remove below line and replace pos with i in your inner loop.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Since the current object position will be same as i */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pos = list.indexOf(current);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < pos; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.get(j).setTimesMoved(1 + current.getTimesMoved());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.remove(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答