Hello, I got an car inventory program that stores the names of cars etc. I thin a class arraylist would work better. Like an array that can "grow" and "shrink". But I do not know how to convert the code from array to arraylist. It should still keep the member methods I'm having there now. The get(), add(), remove(). etc.
Can somebody tell me how to do this? This is just a part of the code. The other parts are fine and don't need to be converted.
Thank you

package assignment1solution;
import java.util.ArrayList;


public class Inventory
{

    private ArrayList<Car> data;

    public Inventory()
    {
        data = new ArrayList<Car>();
    }

   public boolean isExist(String vin)
   {
        if (data.size()>0)
        {
            for(int i=0; i<data.size(); i++)
            {
                if(data.get(i).getVIN().equals(vin))
                {
                    return true;
                }
            }
        }
        else
        {
            return false;
        }
        return false;
   }

    public void addRecord(Car record)
    {
        if ( data.size()<20)
        {
            boolean found = false;
            for(int i=0; i<data.size(); i++)
            {
                if( data.get(i).equals(record) )
                {
                    found = true;
                }
            }

            if( !found && record.getYear()>1980)
            {
                // VIN Check
                data.add(record);                

            }
            else if (found)
            {
                System.out.println("Existing Record: " + record + " not added!");
            }
        }
        else
        {
            System.out.println("Inventory Full!");
        }
    }

    public void removeRecord(String vin)
    {
        if (data.size()>0)
        {
            if( isExist(vin))
            {

                for(int i=0; i<data.size(); i++)
                {
                    if( data.get(i).getVIN().equals(vin) )
                    {
                        data.remove(i);
                        System.out.println("Car removed successfully!");
                        break;
                    }
                }
            }
        }
        else
        {
          System.out.println("No record in the inventory!");
        }
     }

    public void showRecords()
    {
        if (data.size()>0 )
        {
            for(int i=0; i<data.size(); i++)
            {
                System.out.println( data.get(i)  );
            }
        }
        else
        {
          System.out.println("No record in the inventory!");
        }
    }

    public void showRecords(int year, String model)
    {
        if (data.size()>0 )
        {
            for(int i=0; i<data.size(); i++)
            {
                if( (data.get(i).getYear()==year) && (data.get(i).getModel().equals(model) ) )
                {
                    System.out.println( data.get(i)  );
                }
            }
        }
        else
        {
          System.out.println("No record in the inventory!");
        }
    }



}

If your teacher requires that you make your own get, remove, etc, then there is a good chance your teacher also requires that you use your own array - not ArrayList. That being said, if you really want to have your own get method, you could do

public Car get(int index){
return arrayList.get(index);
}

You can figure out the other methods, they are basically identical. Use the ArrayList documentation. But again, what you're doing sounds like a bad idea to me.

If your teacher requires that you make your own get, remove, etc, then there is a good chance your teacher also requires that you use your own array - not ArrayList. That being said, if you really want to have your own get method, you could do

public Car get(int index){
return arrayList.get(index);
}

You can figure out the other methods, they are basically identical. Use the ArrayList documentation. But again, what you're doing sounds like a bad idea to me.

I'm actually teaching it myself. I just want to know Java. That's why I'm still kinda new to this. What do you mean by own array and not arraylist? I'm a little confused now. That's what I found in the book and thought it might work.

Nevermind, I was assuming you had a school assignment. Using ArrayList is perfectly fine. You should know the concepts behind an array though (ArrayList uses an underlying array, and arrays are very important in computer science).

Nevermind, I was assuming you had a school assignment. Using ArrayList is perfectly fine. You should know the concepts behind an array though (ArrayList uses an underlying array, and arrays are very important in computer science).

Well, the book just wants me to convert it, to use class arraylist. And I just don't know how to do it. It seems rather complicated, although I don't think it is. I just thought someone might show me how to do it.

Ok,

int[] array = new int[10];
ArrayList<Integer> list = new ArrayList<Integer>();
for (Integer i: array){
list.add(i);
}

Ok,

int[] array = new int[10];
ArrayList<Integer> list = new ArrayList<Integer>();
for (Integer i: array){
list.add(i);
}

Thank you. Now I just have to modify the member methods to make it work, right?

Also, this is all you need to do there to make it grow and shrink?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.