Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created March 31, 2020 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/5006eb35075b0631bc42b01e2c7ada5d to your computer and use it in GitHub Desktop.
Save feehe21/5006eb35075b0631bc42b01e2c7ada5d to your computer and use it in GitHub Desktop.
public class ArrayLists
{
int[] alist;
int size;
public ArrayLists()
{
alist = new int[0];
size = 0;
}
public void add(int value){
int[] temp = new int[size+1];
for(int i = 0; i<size; i++){
temp[i] = alist[i];
}
temp[size] = value;
alist = temp;
size += 1;
}
public void add(int spot, int value){
int[] temp = new int[size+1];
for(int i = 0; i<size+1; i++){
if(i == spot){
temp[i] = value;
}else if(i < spot){
temp[i] = alist[i];
}else{
temp[i] = alist[i-1];
}
}
alist = temp;
size += 1;
}
public void set(int spot, int value){
alist[spot] = value;
}
public int size(){
return size;
}
public int get(int spot){
return alist[spot];
}
public int remove(int spot){
int[] temp = new int[size-1];
int hold = -1;
for(int i = 0; i<size; i++){
if(i < spot){
temp[i] = alist[i];
}else if(i>spot){
temp[i-1] = alist[i];
}else{
hold = alist[i];
}
}
alist = temp;
size -= 1;
return hold;
}
}
public class arrayListRunner
{
ArrayLists a;
public arrayListRunner(){
a = new ArrayLists();
for(int i = 0; i < 10; i++){
a.add(50-i);
}
print();
a.add(5,200);
a.set(4,500);
a.set(10,300);
print();
a.remove(6);
a.remove(2);
print();
}
public void print(){
for(int i = 0; i < a.size(); i++){
System.out.print(a.get(i)+", ");
}
System.out.println(" ");
System.out.println(a.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment