简体   繁体   中英

Java Array is not being filled correctly

I have the following code that I can't seem to debug. I am adding an object passenger into a seat object that is in a row object. If a seat is occupied it will start from the first row , go through all the seats and add a passenger to it. However if I remove a previously owned seat , then add a new passenger . The new passenger should occupy the previously owned seat . This is not the case, the new passenger will occupy the next seat instead. My assumption is that my firstClassRow.get(i) is starting at the most recent index , rather than 0.

Here's just a basic example

1A filled 1B unfilled(should fill here)
2A filled 2B filled
3A filled 3B unfilled(but fills here)



public void addUser(Passenger p)
{
    Rows tempR;
    boolean result = true;
    int i = 0;


    while(result == true)
    {
        tempR = firstClassRow.get(i);
        if(tempR.check(p) == true)
        {
            tempR.addPassenger(p);
            result = false;
        }
        else
        {
            i = i + 1;
            if (i < number)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
    }
}

Check this example may be helpful

  public static void main(String args[]) {
    String ll[][] = new String[2][2];
    System.out.println(ll.length);
    System.out.println(ll[1].length);

    for (int i = 0; i < ll.length; i++)
        for (int j = 0; j < ll[1].length; j++)
            ll[i][j] = "true";
    System.out.println();
    System.out.println("All seats are occupied");
    display(ll);
    ll[0][1] = "false";
    ll[1][1] = "false";
    System.out.println();
    System.out.println("Two seats emptied");
    display(ll);
    // adding new one
    System.out.println();
    System.out.println("Adding new passenger");
    addPassenger(ll);
    display(ll);

    System.out.println();
    System.out.println("Adding new passenger");
    addPassenger(ll);
    display(ll);
}

static void display(String a[][]) {

    for (int i = 0; i < a.length; i++)
        for (int j = 0; j < a[1].length; j++)
            System.out.println(i + "" + j + " :: " + a[i][j]);
}

static void addPassenger(String a[][]) {
    boolean result = false;
    for (int i = 0; i < a.length; i++)
        for (int j = 0; j < a[1].length; j++) {
            if (a[i][j] == "false") {
                a[i][j] = "true";
                result = true;
            }
            if (result)
                break;
        }
}

 Output:
 All seats are occupied
 00 :: true
 01 :: true
 10 :: true
 11 :: true

 Two seats emptied
 00 :: true
 01 :: false
 10 :: true
 11 :: false

 Adding new passenger
 00 :: true
 01 :: true
 10 :: true
 11 :: false

 Adding new passenger
 00 :: true
 01 :: true
 10 :: true
 11 :: true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM