* Purpose: To create a class to manipulate an array of integers
*/
public class MyArrayList {
private int[] array; //stores an array of integers
private int capacity; //stores the length of the array
private int numElements; //stores the number of elements in the array
//default constructor
public MyArrayList(){
this(10);
}
//overloaded constructor
public MyArrayList(int capas){
this.array = new int[capas];
this.capacity = capas;
this.numElements = 0;
}
//copy constructor
public MyArrayList(MyArrayList list){
this(list.capacity);
//loops through the array
for(int i = 0; i < list.array.length; i++)
this.array[i] = list.array[i];
this.numElements = list.numElements;
}
//appends an integer to the end of the list
public void add(int element){
//determines if helper method needs to be called
if(this.numElements == this.capacity)
this.resize();
this.array[numElements] = element;
this.numElements++;
}
//places an element at the specified index
public void add(int index, int element) throws IndexOutOfBoundsException{
//determines of the specified index is valid
if(!(index < 0 || index > numElements)){
//determines if the helper method needs to be called
if(this.numElements == this.capacity)
this.resize();
//loops through the remainder of the array after specified index
for(int i = index; i this.array[i+1] = this.array[i];
this.array[index] = element;
this.numElements++;
}
else
throw new IndexOutOfBoundsException("Illegal index");
}
//returns the element at a given index
public int get(int index) throws IndexOutOfBoundsException{
//determines of the specified index is valid
if(!(index < 0 || index > numElements))
return(this.array[index]);
else
throw new IndexOutOfBoundsException("Illegal index");
}
//returns index of first location of element (-1 if not found)
public int indexOf(int element){
//loops through the array
for(int i = 0; i < this.array.length; i++)
//determines if the element in the index is the target
if(this.array[i] == element)
return(i);
return(-1);
}
//determines if the list has no elements in it
public boolean isEmpty(){
return(this.numElements == 0);
}
//removes and returns the element in the index specified
public int remove(int index) throws IndexOutOfBoundsException{
//determines of the specified index is valid
if(!(index < 0 || index > numElements)){
int temp = this.array[index];
//loops through the array
for(int i = index; i < this.numElements - 1; i++)
this.array[i] = this.array[i+1];
this.numElements--;
return(temp);
}
else
throw new IndexOutOfBoundsException("Illegal index");
}
//removes first instance of specified element from the array
public boolean removeElement(int element){
//loops through the array
for(int i = 0; i < this.array.length; i++)
//determines if the element in the index is the target
if(this.array[i] == element){
//loops through the array from index of element
for(int j = i; j < numElements - 1; j++)
this.array[j] = this.array[j+1];
this.numElements--;
return(true);
}
return(false);
}
//changes the element at a given position
public void set(int index, int element) throws IndexOutOfBoundsException{
//determines of the specified index is valid
if(!(index < 0 || index > numElements))
this.array[index] = element;
else
throw new IndexOutOfBoundsException("Illegal index");
}
//returns the total number of elements in the array
public int size(){
return(this.numElements);
}
//meaningful representation of an object of class MyArrayList
public String toString(){
String word = "-----";
//determines if the array is empty
if(!this.isEmpty()){
word = "{";
//loops through the array
for(int i = 0; i < this.numElements; i++){
word += this.array[i] + ", ";
}
word = word.substring(0, word.length() - 2);
word += "}";
}
return(word);
}
//helper method to resize the array if necessary
private void resize(){
int[] temp = new int[this.array.length * 2];
//loops through the array
for(int i = 0; i < this.array.length; i++)
temp[i] = this.array[i];
this.array = temp;
this.capacity *= 2;
}
//client program used to test the class
public static void main(String[] args){
MyArrayList list = new MyArrayList();
MyArrayList my = new MyArrayList(5);
System.out.println(list);
System.out.println(my);
list.add(3);
System.out.println(list);
list.add(0, 2);
System.out.println(list);
MyArrayList array = new MyArrayList(list);
System.out.println(array);
System.out.println(array.remove(1));
System.out.println(array);
System.out.println(list.removeElement(2));
System.out.println(list);
System.out.println(list.get(0));
System.out.println(list.indexOf(3));
System.out.println(list.indexOf(5));
System.out.println(my.isEmpty());
System.out.println(my.size());
//adds seven elements to the my object
for(int i = 0; i < 7; i++)
my.add(i);
System.out.println(my.isEmpty());
System.out.println(my.size());
System.out.println(my);
my.set(2, 8);
System.out.println(my);
//all exceptions have been tested and work correctly
}
}
Published by Chris Chen
Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w... View profile
How the LIHEAP Program Can Help You!The LIHEAP program provides a one time payment for those who are senior citizens, low income, or are disabled. It is a very useful program for those who are needing assistance o...- Florida Manatees to Be Removed Off Endangered Species ListThe manatee is on the federal and Florida state endangered species list, but Florida voted to remove it off their list and reduce it to threatened status.
The Web Smiley Turns 25 - Plus a Full List of EmoticonsDid you know where the origins of the web smiley came from? Well, I provide the very interesting history of this staple in electronic communication, plus I list all of the know...- Freelance Writers Can Cash in on the List Article MarketTop 3 reasons to write list articles: you'll make money writing, they're easy to sell, and they spark ideas.
- Choosing a Great Blog ProgramMany people are entering the blogging world. How do you know what is the best blogging program out there? Do you need to spend a lot of money on a blogging program? Read more about popular blogging programs on the mar...
- Your First Java Program
- PHP Array
- JavaScript Array
- A Multidimensional Array for Horizontal Web Page Menus
- Tutorial - Declaring Array in JAVA
- A Guide to Start Programming in Java
- Governor Awards Support Program



