Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package otherfiles.com;
- /*
- This ArrayList class contains two instance variables:
- int element[], and count;
- The following methods are part of the class:
- public void addFirst(int number);
- public void addLast(int number);
- public void insertItemAt(int number,int position);
- public void removeFirst();
- public void removeLast();
- public void removeItemAt(int position);
- public boolean isFound(int number);
- public int getPosition(int number);
- public String toString()
- Some methods are already completed. Complete the other methods
- and test each method to see if they are working correctly.
- */
- public class MyArraylist {
- int[] element;// this is your single dimensional array
- protected int count; // count will be used for monitoring the exact number of elements in element
- // array
- public MyArraylist(int size) {
- // preparing the number of elements the array can hold
- element = new int[size];
- }
- public MyArraylist() {
- // the array is set to hold 10 elements
- // if this constructor is called, the array can hold 10 elements
- this(10);
- }
- public int getFirstElement() {
- return (!isEmpty()) ? element[0] : -1;
- }
- public int getLastElement() {
- return (!isEmpty()) ? element[count - 1] : -1; // ternary operator
- }
- public void doubleTheArray() {
- int[] temp = new int[element.length * 2];
- for (int i = 0; i < element.length; i++)
- temp[i] = element[i];
- element = temp;
- }
- public boolean isEmpty() {
- return count == 0;
- }
- public boolean isFull() {
- return count == element.length;
- }
- /**
- * count must be less than the size of the array, otherwise, you will encounter
- * ArrayIndexOutOfBoundsException this will cause ArrayIndexOutBoundException if
- * array is full,but wont because of the if statement
- */
- public void addLast(int number) {
- if (isFull())
- doubleTheArray();
- element[count++] = number;
- }
- //arr = 9 1 11 12 4 3 4 5 6 7
- //output = 12 4 3 4 5 6 7 9 1 11
- private void shiftLeft(int location) {
- int j;
- int temp = element[0];
- for(int i=0; i<location; i++) {
- //int temp = element[0];
- for(j=0; j<element.length-1; j++) {
- element[j] = element[j+1];
- }
- element[j] = temp;
- }
- }
- private void shiftRight(int location,int number) {
- count++;
- for(int i=element.length-1; i>location; i--) {
- element[i] = element[i-1];
- }
- element[location] = number;
- }
- public void addFirst(int number) {
- if (isFull())
- doubleTheArray();
- if (!isEmpty()) {
- shiftRight(0,number);
- }
- }
- public void removeFirst() {
- int[] newElem = null;
- if (!isEmpty()) {
- newElem = new int[element.length-1];
- for (int i = 0; i < newElem.length; i++) {
- newElem[i] = element[i+1];
- }count--;
- }
- element = newElem;
- }
- public void removeLast() {
- int[] newElem = null;
- if (!isEmpty()) {
- newElem = new int[element.length-1];
- for (int i = 0; i < newElem.length; i++)
- newElem[i] = element[i];
- count--;
- }
- element = newElem;
- }
- public void insertItemAt(int number, int position) {
- count++;
- for(int i=element.length-1; i>position; i--) {
- element[i] = element[i-1];
- }
- element[position] = number;
- }
- public void removeItemAt(int position) {
- count--;
- for(int i=position; i<element.length-1; i++) {
- element[i] = element[i+1];
- }
- }
- public boolean isFound(int number) {
- boolean res = false;
- for(int i=0; i<element.length; i++) {
- if(element[i]==number) {
- return true;
- }
- }
- return res;
- }
- public String toString() {
- // we override toString so that the object of ArrayList will have a String
- // representation, which is to display the elements from the beginning to the
- // end
- StringBuffer sb = new StringBuffer();
- sb.append("[ ");
- for (int i = 0; i < count; i++)
- sb.append(element[i] + " ");
- sb.append("]");
- return sb.toString();
- }
- public int getPosition(int number) {
- int count=0;
- for(int i=0; i<element.length; i++) {
- if(element[i]==number) {
- break;
- }
- count++;
- }
- return count;
- }
- public void doubleTheValue() {
- for (int i = 0; i < count; i++)
- element[i] = element[i] * 2;
- }
- public static void main(String[] args) {
- MyArraylist list = new MyArraylist();
- //list.addLast(0);
- list.addLast(2);
- list.addLast(3);
- list.addLast(4);
- list.addLast(5);
- list.addLast(6);
- list.addLast(7);
- list.addLast(8);
- System.out.println("List contains:" + list);
- list.removeFirst();
- System.out.println("List contains:" + list);
- list.addFirst(7);
- list.addFirst(4);
- list.addFirst(12);
- list.addFirst(1);
- list.addFirst(9);
- list.addFirst(10);
- System.out.println("Add first element:" + list);
- list.insertItemAt(11, 3);
- System.out.println("InsertItem List contains: " + list);
- list.removeFirst();
- System.out.println("RemoveFirst List contains:" + list);
- list.removeLast();
- System.out.println("RemoveLast List contains: " + list);
- list.removeItemAt(5);
- System.out.println("removeItem List contains: " + list);
- list.shiftLeft(3);
- System.out.println("ShiftLeft List contains: " + list);
- System.out.println("Location of 12 is at index: " + list.getPosition(12));
- System.out.println("is 5 found? " + list.isFound(5));
- list.doubleTheValue();
- System.out.println("List contains: " + list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement