Please add the following three methods to our ArrayList class (i.e. StringArrayL
ID: 3753256 • Letter: P
Question
Please add the following three methods to our ArrayList class (i.e. StringArrayList.java):
1.void clear(): This method is used to remove all the elements from any list.
2.int indexOf(String str): The method returns the first occurrence of a given element, or returns -1 in case the element is not on the list.
3.int lastIndexOf(String str): The method returns the last occurrence of a given element, or returns -1 in case the element is not on the list.
import java.util.Arrays;
public class StringArrayList implements myList{
private String[] theData;
private int size = 0;
//the number of elements in the current list
//the next position to add data
private int capacity = 0;
private static final int INITIAL_CAPACITY = 5;
public StringArrayList(){
theData = new String[INITIAL_CAPACITY];
capacity = INITIAL_CAPACITY;
}
@Override
public void add(String value) {
if(size>=capacity){
//full; create a new one
reallocate();
theData[size]=value;
size++;
}else{
theData[size]=value;
size++;
}
}
private void reallocate(){
capacity = capacity * 2;
theData = Arrays.copyOf(theData, capacity);
}
@Override
public void add(int index, String value) {
if(index < 0 || index > size){
throw new
ArrayIndexOutOfBoundsException(index);
}
if(size>=capacity){
reallocate();
}
for(int i = size - 1; i >= index; i--){
theData[i+1] =theData[i];
}
theData[index] = value;
size++;
}
public class Test {
public static void main(String[] args) {
StringArrayList testList = new StringArrayList();
testList.add("Alice");
testList.add("Cher");
testList.add("David");
testList.add("Ellen");
testList.add(1,"Bob");
testList.add("Frank");
testList.remove(1);
testList.set(1, "Joseph");
testList.traverse();
System.out.println("The value at index 2 is " +
testList.get(2));
//testList.set(10, "Hugh");
}
}
Explanation / Answer
import java.util.Arrays;
interface myList {
public void add(String value);
public void add(int index, String value);
public String remove(int index);
public String get(int index);
public void set(int index, String value);
public void clear();
public int indexOf(String str);
public int lastIndexOf(String str);
public void traverse();
}
class StringArrayList implements myList
{
private String[] theData;
private int size = 0;
//the number of elements in the current list
//the next position to add data
private int capacity = 0;
private static final int INITIAL_CAPACITY = 5;
public StringArrayList()
{
theData = new String[INITIAL_CAPACITY];
capacity = INITIAL_CAPACITY;
}
public void traverse()
{
if(size!=0)
{
System.out.print(" [");
for(int i=0;i<size;i++)
System.out.print(theData[i]+",");
System.out.println("]");
}
}
public void clear()
{
for(int i=0;i<size;i++)
theData[i] = "";
size=0;
}
public String remove(int index)
{
size--;
return theData[index];
}
public int indexOf(String str)
{
int i=0;
for(i=0;i<size;i++)
{
if(theData[i].equals(str))
break;
}
return i;
}
public int lastIndexOf(String str)
{
int i=0;
for(i=size;i>=0;i--)
{
if(theData[i].equals(str))
break;
}
return i;
}
public String get(int index)
{
return theData[index];
}
public void set(int index, String value)
{
theData[index] = value;
}
@Override
public void add(String value)
{
if(size>=capacity){
//full; create a new one
reallocate();
theData[size]=value;
size++;
}
else{
theData[size]=value;
size++;
}
}
private void reallocate()
{
capacity = capacity * 2;
theData = Arrays.copyOf(theData, capacity);
}
@Override
public void add(int index, String value) {
if(index < 0 || index > size)
{
throw new ArrayIndexOutOfBoundsException(index);
}
if(size>=capacity){
reallocate();
}
for(int i = size - 1; i >= index; i--){
theData[i+1] =theData[i];
}
theData[index] = value;
size++;
}
}
public class Test {
public static void main(String[] args) {
StringArrayList testList = new StringArrayList();
testList.add("Alice");
testList.add("Cher");
testList.add("David");
testList.add("Ellen");
testList.add("David");
testList.add(1,"Bob");
testList.add("David");
testList.add("Frank");
testList.remove(1);
testList.set(1, "Joseph");
testList.traverse();
System.out.println("The value at index 2 is " +
testList.get(2));
System.out.println("First Index = "+testList.indexOf("David"));
System.out.println("Last Index = "+testList.lastIndexOf("David"));
testList.clear();
System.out.println("After Clearing :");
testList.traverse();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.