tags:

views:

67

answers:

1

I have added this as my arraylist and under my inventory list I keep getting errors. What am I doing wrong.

import java.util.Arrays;

public class MyArrayList {    
String[] strings = new String[2];
int size = 0;

public void add(String str) {
    if (size == strings.length) {
        // Double the size
        strings = Arrays.copyOf(strings, strings.length * 2);
    }

    strings[size++] = str;
}

public String get(int i) {
    if (i >= size)
        throw new ArrayIndexOutOfBoundsException();
    return strings[i];
}

public void remove(int i) {
    size--;
    for (int j = i; j < size; j++)
        strings[j] = strings[j+1];
  }
 }

I get errors:

 public boolean add(String title, int productID)
 {
    if( numberOfRecords == list.length )// list.length it says cannot find variable length
    {
        System.out.println("Not enough space! Not added: " + title);
        return false;
    }
    else
    {
        list[numberOfRecords] = new MyArrayList(title,productID );//it says array required
        numberOfRecords++;
        return true;
    }
 }

 public MyArrayList getMovie(int index)
 {
     return list[index];//it says array required
 }

 public int getIndex(int productID)
 {
     int index = -1;
     for(int i=0; i<numberOfRecords; i++)
     {
        if( list[i].getProductID() == productID )// it says array required
        {
            index = i;
        }
     }

     for(int i=0; i<numberOfRecords; i++)
     {
         status ="Available";
         if( list[i].getIsReserved())// it says array required
         {
             status = "Reserved";
         }
         else if( list[i].getIsRented())// it says array required
         {
             status = "Rented";
         }

         // why there is minus(-)
         System.out.printf("%3d. %-25s %-9s\n", list[i].getProductID(), list[i].getTitle(),status  );// it says array required

     }
 }

}

public class Main {

public static void main(String[] args) {

Inventory netFlix = new Inventory(8);

    netFlix.add( "Prince of Persia",     140);
    netFlix.add( "Clash of Titans",      223);
    netFlix.add( "Avatar",               353);
    netFlix.add( "Inception",            460);
    netFlix.add( "Resident Evil",        105);
    netFlix.add( "Devil",                624);
    netFlix.add( "Memento",              117);
    netFlix.add( "D2: The Mighty Ducks", 508);
    netFlix.add( "The Lord of the Rings",910);
    netFlix.add( "The Uninvited",        120);

    Menu application = new Menu(netFlix);

    application.run();
}

}

+1  A: 

I suppose that list is in fact a List.

To get the size of a List you have to use the size() method. The length attribute only exists on arrays.

To access an element of a List you have to use the method get(index) instead of [index] as you do on an array.

To add an element to a List the method is add(element) or add(index, element) instead of [index] = element.


Resources :

Colin Hebert
I did the changes but I still get errors
Lola