Skip to content

Instantly share code, notes, and snippets.

@yaswanthrajyadiki
Created September 4, 2015 11:25
Show Gist options
  • Save yaswanthrajyadiki/49280be82ec4ba831c6e to your computer and use it in GitHub Desktop.
Save yaswanthrajyadiki/49280be82ec4ba831c6e to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class DynamicArray<T> implements MyArrayList<T> {
int end;
T[] array;
DynamicArray() {
end = -1;
array = (T[])new Object[10];
}
public void add(T element) {
end++;
if (end < array.length - 1) {
array[end] = element;
} else {
int newSize = 2 * array.length;
T[] newArray = (T[])new Object[newSize];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
array[end] = element;
}
}
public void removeIndex(int index) {
for (int i = 0; i <= end; i++) {
if (index == i) {
for (int j = i; j <= end - 1; j++) {
array[j] = array[j + 1];
}
end--;
}
}
}
public void removeElement(T element) {
for (int i = 0; i <= end; i++) {
if (array[i] == element) {
for (int j = i; j <= end - 1; j++) {
array[j] = array[j + 1];
}
end--;
}
}
}
public void read(int index) {
if (index <= end) {
System.out.println(array[index]);
}
}
public void modifyIndex(int index, T element) {
for (int i = 0; i <= end; i++) {
if (index == i) {
array[i] = element;
}
}
}
public void modifyElement(T element1, T element2) {
for (int i = 0; i <= end; i++) {
if (array[i] == element1) {
array[i] = element2;
}
}
}
public void print() {
for (int i = 0; i < end; i++) {
System.out.print(array[i] + ",");
}
System.out.println(array[end]);
}
}
import java.util.Scanner;
class DynamicArrayDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String command = sc.nextLine();
if (command.equalsIgnoreCase("I")) {
DynamicArray<Integer> da = new DynamicArray<Integer>();
command = sc.nextLine();
while(!command.equals("end")) {
String splitC[] = command.split(" ");
if (splitC[0].equalsIgnoreCase("add")) {
da.add(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("removeIndex")) {
da.removeIndex(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("removeElement")) {
da.removeElement(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("modifyIndex")) {
da.modifyIndex(Integer.parseInt(splitC[1]), Integer.parseInt(splitC[2]));
} else if (splitC[0].equalsIgnoreCase("modifyElement")) {
da.modifyElement(Integer.parseInt(splitC[1]), Integer.parseInt(splitC[2]));
} else if (splitC[0].equalsIgnoreCase("read")) {
da.read(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("print")) {
da.print();
}
command = sc.nextLine();
}
} else if (command.equalsIgnoreCase("C")) {
DynamicArray<Character> da1 = new DynamicArray<Character>();
command = sc.nextLine();
while(!command.equals("end")) {
String splitC[] = command.split(" ");
if (splitC[0].equalsIgnoreCase("add")) {
da1.add(splitC[1].charAt(0));
} else if (splitC[0].equalsIgnoreCase("removeIndex")) {
da1.removeIndex(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("removeElement")) {
da1.removeElement(splitC[1].charAt(0));
} else if (splitC[0].equalsIgnoreCase("modifyIndex")) {
da1.modifyIndex(Integer.parseInt(splitC[1]), splitC[2].charAt(0));
} else if (splitC[0].equalsIgnoreCase("modifyElement")) {
da1.modifyElement(splitC[2].charAt(0), splitC[2].charAt(0));
} else if (splitC[0].equalsIgnoreCase("read")) {
da1.read(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("print")) {
da1.print();
}
command = sc.nextLine();
}
} else if (command.equalsIgnoreCase("S")) {
DynamicArray<String> da2 = new DynamicArray<String>();
command = sc.nextLine();
while(!command.equals("end")) {
String splitC[] = command.split(" ");
if (splitC[0].equalsIgnoreCase("add")) {
da2.add(splitC[1]);
} else if (splitC[0].equalsIgnoreCase("removeIndex")) {
da2.removeIndex(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("removeElement")) {
da2.removeElement(splitC[1]);
} else if (splitC[0].equalsIgnoreCase("modifyIndex")) {
da2.modifyIndex(Integer.parseInt(splitC[1]), splitC[2]);
} else if (splitC[0].equalsIgnoreCase("modifyElement")) {
da2.modifyElement(splitC[2], splitC[2]);
} else if (splitC[0].equalsIgnoreCase("read")) {
da2.read(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("print")) {
da2.print();
}
command = sc.nextLine();
}
} else if (command.equalsIgnoreCase("D")) {
DynamicArray<Double> da3 = new DynamicArray<Double>();
command = sc.nextLine();
while(!command.equals("end")) {
String splitC[] = command.split(" ");
if (splitC[0].equalsIgnoreCase("add")) {
da3.add(Double.parseDouble(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("removeIndex")) {
da3.removeIndex(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("removeElement")) {
da3.removeElement(Double.parseDouble(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("modifyIndex")) {
da3.modifyIndex(Integer.parseInt(splitC[1]),
Double.parseDouble(splitC[2]));
} else if (splitC[0].equalsIgnoreCase("modifyElement")) {
da3.modifyElement(Double.parseDouble(splitC[2]),
Double.parseDouble(splitC[2]));
} else if (splitC[0].equalsIgnoreCase("read")) {
da3.read(Integer.parseInt(splitC[1]));
} else if (splitC[0].equalsIgnoreCase("print")) {
da3.print();
}
command = sc.nextLine();
}
}
// DynamicArray<Integer> da = new DynamicArray<Integer>();
// da.add(1);
// da.add(2);
// da.add(3);
// da.add(4);
// da.add(3);
// da.add(5);
// da.add(6);
// da.removeIndex(5);
// da.read(5);
// da.modifyElement(3,4);
// da.modifyIndex(5,5);
// da.print();
}
}
public interface MyArrayList<T> {
void add(T element);
void removeIndex(int index);
void removeElement(T element);
void modifyIndex(int index, T element);
void modifyElement(T element1, T element2);
void read(int index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment