第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

根據(jù)之前的 ELEMENTS 遞增 ArrayElement 參數(shù)

根據(jù)之前的 ELEMENTS 遞增 ArrayElement 參數(shù)

藍(lán)山帝景 2022-05-12 16:36:29
我是 java 新手,但一直在玩數(shù)組列表,現(xiàn)在卡住了。我從一個(gè)名為 Car 的類中創(chuàng)建了一個(gè)數(shù)組列表,其中包含三個(gè)參數(shù),其中一個(gè)稱為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;}
查看完整描述

2 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

第一種方法,第 8 行。為什么要pos在i. int pos = list.indexOf(i);會(huì)給-1。


另外,在這個(gè)循環(huán)中


for ( int j = 0 ; pos < j ; j++)

    {

          current.setTimesMoved(1 + current.getTimesMoved());

    }

你總是指向數(shù)組列表中的同一個(gè)元素。


相反,您可能希望像下面這樣對(duì)其進(jìn)行編程:


for ( int j = 0; j<i; j++)

    {

          Car car = list.get(j);

          car.setTimesMoved(1 + car.getTimesMoved());

    }


查看完整回答
反對(duì) 回復(fù) 2022-05-12
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊

您的方法中有以下問(wèn)題,

  • 首先你得到一個(gè)整數(shù)而不是對(duì)象的索引。

  • 您的內(nèi)部 for 循環(huán)條件是錯(cuò)誤的。

  • 您總是增加當(dāng)前對(duì)象的值而不是以前的值。

我已經(jīng)用你的方法糾正了它們。使用以下一種,

public void carDepart() {

        for (int i = 0; i < list.size(); i++) {

            Car current = list.get(i);   // get next car

            if (current.getStatus().equals("DEPART")) {

                /* You can remove below line and replace pos with i in your inner loop. 

                Since the current object position will be same as i */

                int pos = list.indexOf(current);


                for (int j = 0; j < pos; j++) {

                    list.get(j).setTimesMoved(1 + current.getTimesMoved());

                }


                list.remove(i);

                return;

            }


        }

    }


查看完整回答
反對(duì) 回復(fù) 2022-05-12
  • 2 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)