Is there a way to fix this? I keep getting an Off one error : -1 Is there a way
ID: 3767185 • Letter: I
Question
Is there a way to fix this?
I keep getting an Off one error : -1
Is there a way to dynamically resize my arraylist?
How can I implement the Iterable interface?
-------------------------------------------------------------------------------------------------------------
public class ArrayList {
private Object[] data;
private int size;
private int capacity = 10;
public ArrayList() {
data = new Object[capacity];
size = 0;
}
public void insert(Object other, int index) {
if (index < 0 || index > size() || other == null) {
System.out.println("Cannot insert");
System.exit(0);
}
if (index < data.length) {
// add the object into the data here
data[index] = other;
size++;
} else {
//else if the index is out of range we need to resize
Object[] temp = new Object[capacity];
capacity = capacity * 2;
for (int i = 0; i < data.length; i++) {
temp[i] = data[i];
}
}
}
public Object remove(int index) {
if (index >= size || index < 0) {
System.out.println("List is empty: cannot delete");
return 0;
}
Object removedElements = data[index]; //copy of current class array.
for (int i = index; i <= size; i++) {
data[i] = data[i + 1];
}
size--; //reduce size of Array after removal of element.
return removedElements;
}
public int size() {
return size;
}
public String toString() {
String values = "";
for (int i = 0; i < size() - 1; i++) {
values += (data[i] + " ");
}
return values;
}
public boolean isEmpty() {
return size() == 0;
}
public int indexOf(Object element) {
for (int i = 0; i < size(); i++) {
if (data[i] == element) return i;
}
return -1;
}
public boolean equals(Object[] other) {
if (other == null) {
return false;
}
for (int i = 0; i < size(); i++) {
if (!data[i].equals(other[i])) {
return false;
}
}
return true;
}
public Object get(int index) {
if (index < 0 || index >= size) {
System.out.println("index out of bounds");
}
Object obj = data[index];
return obj;
}
}
Explanation / Answer
import java.util.Arrays;
public class ArrayList {
public static void main(String[] args) {
try {
ArrayList arrayList = new ArrayList();
arrayList.insert("Haii0", 0);
arrayList.insert("Haii1", 1);
arrayList.insert("Haii2", 2);
arrayList.insert("Haii3", 3);
arrayList.insert("Haii4", 4);
arrayList.insert("Haii5", 5);
arrayList.insert("Haii6", 6);
arrayList.insert("Haii7", 7);
arrayList.insert("Haii8", 8);
arrayList.insert("Haii9", 9);
arrayList.insert("Haii10", 10);
arrayList.insert("Haii11", 11);
System.out.println(arrayList.get(11));
} catch (Exception e) {
e.printStackTrace();
}
}
private Object[] data;
private int size;
private int capacity = 10;
private void ensureCapa() {
size = data.length * 2;
data = Arrays.copyOf(data, size);
}
public ArrayList() {
data = new Object[capacity];
size = 0;
}
public void insert(Object other, int index) {
if (index < 0 || other == null) {
System.out.println("Cannot insert");
System.exit(0);
}
// if the size lessthan index then ensure capacity
if (index >= size()) {
ensureCapa();
}
if (index < data.length) {
// add the object into the data here
data[index] = other;
size++;
} else {
// else if the index is out of range we need to resize
Object[] temp = new Object[capacity];
capacity = capacity * 2;
for (int i = 0; i < data.length; i++) {
temp[i] = data[i];
}
}
}
public Object remove(int index) {
if (index >= size || index < 0) {
System.out.println("List is empty: cannot delete");
return 0;
}
Object removedElements = data[index]; // copy of current class array.
for (int i = index; i <= size; i++) {
data[i] = data[i + 1];
}
size--; // reduce size of Array after removal of element.
return removedElements;
}
public int size() {
return size;
}
public String toString() {
String values = "";
for (int i = 0; i < size() - 1; i++) {
values += (data[i] + " ");
}
return values;
}
public boolean isEmpty() {
return size() == 0;
}
public int indexOf(Object element) {
for (int i = 0; i < size(); i++) {
if (data[i] == element)
return i;
}
return -1;
}
public boolean equals(Object[] other) {
if (other == null) {
return false;
}
for (int i = 0; i < size(); i++) {
if (!data[i].equals(other[i])) {
return false;
}
}
return true;
}
public Object get(int index) {
if (index < 0 || index >= size) {
System.out.println("index out of bounds");
}
Object obj = data[index];
return obj;
}
}
Note:
please check working fine
and if you want grow your list dynamically use the method ensureCapa(); in the insert() method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.