简体   繁体   中英

Why does my object get deleted from array?

I'm writing a program in Netbeans which I today realized didn't work as I wanted it to. The idea of the program is to take what the user put in, create an object with the data and then store this object in an array. This happens when pressing a button. When pressing another button the content of the array is displayed on an jTextArea. The problem with this, I have now realized, is that the object that gets added to the array, gets removed when adding another object and I can't figure out why.

This is my class for creating objects:

public class Car {
    public String brand;
    public String year;

  public Car (String brand, String year) {
      this.brand = brand;
      this.year = year;
   }

}

And this the the code:

int b = 1;
Car[] carArray = new Car[b];

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
// TODO add your handling code here:
    int a = 0;

    carArray[a] = new Car (txtfBrand.getText(), txtfYear.getText());
    a++;
    b++;
}                                       

private void btnReadActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    for (int i = 0; i < carArray.length; i++) {
        txtaRead.append("" + carArray[i].Brand + "\n" + carArray[i].Year + "\n\n");
        lblSize.setText("" + carArray.length);
    }
}

What the code above does is to create an array, carArray , which is one Car -object big. When the user then presses the button btn a Car is created with the values from txtfBrand and txtfYear and stored in the carArray on index 0. The array carArray is then increased to be two Car -objects big, and the next object that is created is stored at index 2 and so on... Or so I thought the code worked...

But when I press the read -button, only the last object I created is displayed which must mean that it's only one object in the carArray . When printing out the size of the carArray on the lblSize , it is always 1. I don't understand why this happens, and any help would be greatly appreciated!

There problem is

int b = 1;
Car[] carArray = new Car[b];

when you give b in new Car[b] , you are saying java to create an array of size b which is 1. That is why size of your array size is always 1.

Even if you are doing b++ , it does not add extra space to the array because your array is already created. So it is always be of size 1 .

Also every time you are replacing the value at 0th index with( which was not intentional (I guess) but which is correct for an array of size 1 ):

int a = 0;
carArray[a] = new Car (txtfBrand.getText(), txtfYear.getText());

When you don't know how many elements you are going to insert, you should use ArrayList or LinkedList .

This is because you're setting a to zero within your add method. So the object gets placed at the first index, erasing whichever item was there before. Moving your variable initialization outside of the method should correct this problem, ensuring accurate increments.

int a = 0;
int b = 1;
Car[] carArray = new Car[b];

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    carArray[a] = new Car (txtfBrand.getText(), txtfYear.getText());
    a++;
    b++;
}    

Including the below comment exchange:

Be careful about your array initialization as well. You're creating carArray with size of 1 (due to b = 1; ), so you may encounter an ArrayIndexOutOfBounds Exception if you try to add more elements than you have space.

Additionally, it looks like you're trying to make carArray bigger after each new item is added. I suggest looking into using an ArrayList if you want an array which can change size. Even if you change the value of b , carArray will always have a length of 1

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