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 Arraylist {
- 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 Arraylist(int size) {
- // preparing the number of elements the array can hold
- element = new int[size];
- }
- public Arraylist() {
- // 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;
- }
- private void shiftLeft(int loc) {
- }
- private void shiftRight(int loc) {
- int pos = count;
- while (loc < pos)
- element[pos] = element[--pos];
- }
- public void addFirst(int number) {
- if (isFull())
- doubleTheArray();
- if (!isEmpty())
- shiftRight(0);
- element[count++] = number;
- }
- public void insertItemAt(int number, int position) {
- }
- public int[] 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];
- return newElem;
- }
- public int[] removeLast() {
- int[] newElem = null;
- if (!isEmpty())
- newElem = new int[element.length-1];
- for (int i = 0; i < newElem.length; i++)
- newElem[i] = element[i];
- return newElem;
- }
- public void removeItemAt(int position) {
- }
- public boolean isFound(int number) {
- return false;
- }
- 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) {
- return number;
- }
- public void doubleTheValue() {
- for (int i = 0; i < count; i++)
- element[i] = element[i] * 2;
- }
- public static void main(String[] args) {
- Arraylist list = new Arraylist();
- 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.addFirst(7);
- //System.out.println("Add first element:" + list);
- //list.removeFirst();
- //System.out.println("List contains:" + list);
- //list.addFirst(7);
- //list.addFirst(4);
- //list.addFirst(12);
- //System.out.println("List contains:" + list);
- // Expected output: 9 4 7 5 7 8 9
- /// System.out.println("List contains: " + list);
- // list.insertItemAt(10, 3);
- // System.out.println("List contains:" + list);
- //list.removeFirst();
- //System.out.println("List contains:" + list);
- list.removeLast();
- System.out.println("RemoveLast List contains: " + list);
- // list.removeItemAt(5);
- //// System.out.println("List contains: " + list);
- // list.doubleTheValue();
- // System.out.println("List contains: " + list);
- // System.out.println("is 5 found? " + list.isFound(5));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement