Created
April 1, 2020 15:22
-
-
Save raunaqsingh2020/c42d86fdfe704c7f9d9dac52782d262f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayList { | |
int[] arrList; | |
int size; | |
public ArrayList () { | |
size = 0; | |
arrList = new int[0]; | |
} | |
public void add(int value) { | |
size += 1; | |
int[] temp = new int[size]; | |
for (int a=0; a < arrList.length; a++) { | |
temp[a] = arrList[a]; | |
} | |
temp[size - 1] = value; | |
arrList = temp; | |
} | |
public void add(int index, int value) { | |
size += 1; | |
int [] list = new int[size]; | |
for (int a=0; a < size; a++) { | |
if (a < index) { | |
list[a] = arrList[a]; | |
} | |
if (a == index) { | |
list[a] = value; | |
} | |
if (a > index) { | |
list[a] = arrList[a - 1]; | |
} | |
} | |
arrList = list; | |
} | |
public void set (int index, int value) { | |
arrList[index] = value; | |
} | |
public void remove (int spot) { | |
size -= 1; | |
int[] list = new int[size]; | |
for (int a=0; a < arrList.length; a++) { | |
if (a != spot) { | |
if (a < spot) { | |
list[a] = arrList[a]; | |
} | |
if (a > spot) { | |
list[a - 1] = arrList[a]; | |
} | |
} | |
} | |
arrList = list; | |
} | |
public int size() { | |
return size; | |
} | |
public void print() { | |
String string = ""; | |
for (int a: arrList) { | |
string += a; | |
string += ", "; | |
} | |
string = string.substring(0, string.length()-2); | |
System.out.println(string); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment