Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a1;
- import java.util.Scanner;
- import CustomArrayList.StaticArrayList_Main;
- public class CustomArrayList {
- private Object[] arr;
- private int count;
- private static final int INITIAL_CAPACITY = 4;
- public CustomArrayList() {
- arr = new Object[INITIAL_CAPACITY];
- count = 0;
- }
- public int getLength() {
- return count;
- }
- public void add(Object item) {
- add(count, item);
- }
- public void add(int index, Object item) {
- if (index>count || index<0) {
- throw new IndexOutOfBoundsException(
- "Invalid index: " + index);
- }
- Object[] extendedArr = arr;
- if (count + 1 == arr.length) {
- extendedArr = new Object[arr.length * 2];
- }
- System.arraycopy(arr, 0, extendedArr, 0, index);
- count++;
- System.arraycopy(
- arr, index, extendedArr, index+1, count-index-1);
- extendedArr[index] = item;
- arr = extendedArr;
- }
- public int indexOf(Object item) {
- if (item == null) {
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] == null)
- return i;
- }
- } else {
- for (int i = 0; i < arr.length; i++)
- if (item.equals(arr[i]))
- return i;
- }
- return -1;
- }
- public void clear() {
- arr = new Object[0];
- count = 0;
- }
- public boolean contains(Object item) {
- int index = indexOf(item);
- boolean found = (index != -1);
- return found;
- }
- public Object elementAt(int index) {
- if (index>=count || index<0) {
- throw new IndexOutOfBoundsException(
- "Invalid index: " + index);
- }
- return arr[index];
- }
- public Object remove(int index) {
- if (index>=count || index<0) {
- throw new IndexOutOfBoundsException(
- "Invalid index: " + index);
- }
- Object item = arr[index];
- System.arraycopy(arr, index+1, arr, index, count-index+1);
- arr[count - 1] = null;
- count--;
- return item;
- }
- public int remove(Object item) {
- int index = indexOf(item);
- if (index == -1) {
- return index;
- }
- System.arraycopy(arr, index+1, arr, index, count-index+1);
- count--;
- return index;
- }
- public static void main(String[] args){
- CustomArrayList shoppingList = new CustomArrayList();
- shoppingList.add("Milk");
- shoppingList.add("Honey");
- shoppingList.add("Olives");
- shoppingList.add("Beer");
- shoppingList.remove("Olives");
- System.out.println("We need to buy:");
- for(int i=0; i<shoppingList.getLength(); i++) {
- System.out.println(shoppingList.elementAt(i));
- }
- System.out.println("Do we have to buy Bread? " +
- shoppingList.contains("Bread"));
- CustomArrayList shoppingList1 = new CustomArrayList();
- Scanner sc=new Scanner(System.in);
- while(true)
- {
- System.out.println();
- System.out.println("For ADD choose 1, for REMOVE choose 2, for END choose 3");
- int choice = sc.nextInt();
- if(choice==1)
- {
- System.out.println("The element you want to add:");
- String n=sc.next();
- if(shoppingList1.contains(n)==false)
- {
- shoppingList1.add(n);
- System.out.println();
- System.out.println("Now your list contains:");
- getList(shoppingList1);
- continue;
- } else
- {
- System.out.println("The element is already in the list");
- continue;
- }
- }
- if(choice==2)
- {
- System.out.println("The element you want to remove:");
- String n=sc.next();
- if(shoppingList1.contains(n))
- {
- shoppingList1.remove(shoppingList1.indexOf(n));
- System.out.println();
- System.out.println("Now your list contains:");
- getList(shoppingList1);
- continue;
- } else
- {
- System.out.println("No element to remove");
- continue;
- }
- }
- if(choice==3)
- {
- System.out.println();
- System.out.println("Now your list contains:");
- getList(shoppingList1);
- System.out.println("Program ended");
- break;
- }
- } // end while
- sc.close();
- } // end MAIN
- static void getList(CustomArrayList shoppingList)
- {
- for(int i=0; i<shoppingList.getLength(); i++)
- {
- System.out.println(shoppingList.elementAt(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement