Objective: Become familiar with the dynamic allocation of array storage Become f
ID: 3563003 • Letter: O
Question
Objective:
Become familiar with the dynamic allocation of array storage
Become familiar with ArrayList
Description:
Create your myIntArrayList class that is functionally similar to the formal Java ArrayList class.
Requirements:
Your myIntArrayList should contain the following methods and characteristics:
(2 point) Should contain a default constructor myIntArrayList()
(2 point) Should contain an alternative constructor myIntArrayList(int[] anArray) that allows the user to create an initialmyIntArrayList that is equivalent to an array that is passed to the constructor
(2 points) Should contain an alternative constructor myIntArrayList(myIntArrayList anExample) that will create an intMyArrayList identical to one that is passed to it. This is also called the copy contructor.
(4 points)Should contain a void add(int index, int value) method
Need to check for appropriate index. If index is outside of the array boundaries, then add at the beginning or the end
(4 points)Should contain a int remove(int index) method
If the index is outside of the array boundaries, then remove the first or last element
(3 points) Should contain a myIntArrayList simpleSort() method that will return another myIntArrayList object with the contents of the current object sorted using an insertion sort.
(2 points) Should contain a boolean equals(myIntArrayList anotherList) method that will return 1 if the two ArrayLists are equal, otherwise it returns 0.
(2 points) Should contain a boolean isCongruent(myIntArrayList anotherList) method that will return 1 if the two ArrayLists are congruent, otherwise it returns 0.
(2 points)Should contain a int get(int index) method
If index is out of boundary then return either the first or last element
(2 points) Should contain an int indexOf(int value) method
Returns the index of the first occurance of value
(1 points) Should contain a int size() method
(2 points) Should contain a void clear() method that empties the myIntArryList
(2 points) Should contain a void print() method that prints all elements of the myIntArrayList in one row delimited by one space
Explanation / Answer
myIntArrayList.java
import java.util.ArrayList;
import java.util.Arrays;
public class myIntArrayList {
private int[] anArray;
private int n;
public myIntArrayList(){
}
public myIntArrayList(int[] anArray){
this.anArray = anArray.clone();
n = anArray.length;
}
public myIntArrayList(myIntArrayList anExample){
this.anArray = anExample.getAll().clone();
}
private int[] getAll(){
return anArray.clone();
}
public void add(int index, int value){
if(index >= anArray.length || index<0){
anArray = Arrays.copyOf(anArray,n+1);
anArray[n-1] = value;
}
else{
anArray = Arrays.copyOf(anArray,n+1);
for (int i = n; i > index; i--) {
anArray[i] = anArray[i-1];
}
anArray[index] = value;
}
n = anArray.length;
}
public void remove(int index){
if(index >= anArray.length || index<0){
anArray = Arrays.copyOf(anArray,n-1);
}
else{
for (int i = index; i < n-1; i++) {
anArray[i] = anArray[i+1];
}
anArray = Arrays.copyOf(anArray,n-1);
}
n = anArray.length;
}
public myIntArrayList simpleSort(){
int[] numbers = anArray.clone();
int i,j,temp;
for(i=1;i<n;i++){
temp = numbers[i];
for(j=i-1;j>=0;j--){
if(numbers[j]<temp)
break;
numbers[j+1] = numbers[j];
}
numbers[j+1] = temp;
}
return new myIntArrayList(numbers);
}
public boolean equals(myIntArrayList anotherList){
int[] second = anotherList.getAll();
if(second.length != n)
return false;
else{
for (int i = 0; i < n; i++) {
if(anArray[i] != second[i])
return false;
}
return true;
}
}
public int get(int index){
if(index >= anArray.length || index<0){
return anArray[0];
}
return anArray[index];
}
public int indexOf(int value){
for (int i = 0; i < n; i++) {
if(anArray[i] == value)
return i;
}
return -1;
}
public int size(){
return n;
}
public void clear(){
anArray = Arrays.copyOf(anArray,0);
n = anArray.length;
}
public void print(){
System.out.println();
for(int i : anArray){
System.out.print(i + " ");
}
System.out.println();
}
}
Please note that the method for congruency was not implemented. I was not sure about the exact functionality of it. Thank you :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.