Я новичок в java и пытаюсь перенести свой опыт из мира c # на java вот код:
public class TestBasket {
private Item[] shops = {} ;
int arraysIndex=0;
public static void main(String argc[]){
TestBasket tb = new TestBasket();
try{
tb.storeItems(new Item("test", 100));
}
catch(Exception e){
System.out.println("Error");
System.out.println(e.toString());
}
}
public void storeItems(Item it){
if (arraysIndex >= shops.length){
///resizeArray(shops);
System.out.println("the count of length is" + shops.length);
cpArr(shops);
System.out.println("the count of length is" + shops.length);
}
shops[arraysIndex] = it;
arraysIndex++;
}
//this is a generic method to resize every kind of array
public Item[] cpArr(Item[] arr){
Item[] retArr = Arrays.copyOf(arr, arr.length + 10);
return retArr;
}
}
после выполнения программы я получаю это сообщение:
length is0
length is0
Error
java.lang.ArrayIndexOutOfBoundsException: 0
, что означает, что длина массива по-прежнему равна нулю, а она не должна быть равна нулю. Я не понимаю, где я ошибся?
с уважением.
Я получаю ответ, это моя вина, что я получил значение повторного выполнения, чего я не сделал.