Question: MyArrayL... MyStrin... package MyImplementations; public class MyString { private char [ ] chars; public MyString ( char [ ] chars ) { this.chars = chars.clone

MyArrayL...
MyStrin...
package MyImplementations;
public class MyString {
private char[] chars;
public MyString(char[] chars){
this.chars = chars.clone();
}
public MyString(String s){
this.chars = s.toCharArray();
}
public int length(){
return chars.length;
}
public char charAt(int index){
if (index 0|| index >= chars.length) throw new IndexOutOfBoundsException();
return chars[index];
}
public MyString substring(int begin, int end){
if (begin 0|| end > chars.length || begin > end) throw new IndexOutOfBoundsException();
char[] subChars = new char[end - begin];
System.arraycopy (chars, begin, subChars, 0, end - begin);
return new MyString(subChars);
}
public MyString concat(MyString s){
char[] result = new chalr[chars.length + s.length()];
System.arraycopy(chars,0, result, 0, chars.length);
System.arraycopy(s.chars, 0, result, chars.length, s.length());
return new MyString(result);
}
public String toString(){
return new String(chars);
}
public int indexOf(char c){
for (int i =0; i chars.length; i++){
if (chars[i]== c){
return i;
}
}
return -1;
}
public MyString replace(char oldChar, char newChar){
for (int i =0; i chars.length; i++){
if (chars[i]== oldChar){
chars[i]= newChar;
}
}
return this;
}
return -1; } public MyString replace(char oldChar, char newChar){ for (int i =0; i chars.length; i++){ if (chars[i]== oldChar){ chars[i]= newChar; }} return this; } public int count(char c){ int count =0; for (int i =0; i chars.length; i++){ if (chars[i]== c){ count++; }} return count; }}
MyArra... MyString....
package MyImplementations;
public class MyArrayList implements MyList {
public static final int INITIAL CAPACITY =16;
@SuppressWarnings("unchecked")
private E[] data =(E[]) new Object[INITIAL_CAPACITY];
private int size =0;
public MyArrayList(){}
@SuppressWarnings("unchecked")
public MyArrayList(int capacity){
data =(E[]) new Object[capacity];
}
@Override
public void add(int index, E e){
if (index 0|| index > size) throw new IndexOutOfBoundsException();
ensureCapacity();
for (int i = size -1; i >= index; i--){
data[i +1]= data[i];
}
data[index]= e;
size++;
}
private void ensureCapacity(){
if (size >= data.length){
@SuppressWarnings("unchecked")
E[] newData =(E[]) new Object[data.length *2+1];
System.arraycopy(data,0, newData, 0, size);
data = newData;
}
}
@Override
public E get(int index){
if (index 0|| index >= size) throw new IndexOutOfBoundsException();
return data[index];
}
@Override
public E set(int index, E e){
if (index 0|| index >= size) throw new IndexOutOfBoundsException();
E old = data[index];
data[index]= e;
return old;
}
@Override
public int size(){
return size;
}
@Override
public void clear(){
for (int i =0; i size; i++){
data[i]= nul.l:
MyArra... MyString....
52|||\turn stze;
}
@Override
public void clear(){
for (int i =0; i size; i++){
data[i]= null;
}
size =0;
}
@Override
public boolean contains(Object e){
return indexOf(e)>=0;
}
@Override
public int indexOf(Object e){
for (int i =0; i size; i++){
if (e == null ? data[i]== null : e.equals(data[i])) return i;
}
return -1;
}
@Override
public int lastIndexOf(E e){
for (int i = size -1; i >=0; i--){
if (e == null ? data[i]== null : e.equals(data[i])) return i;
}
return -1;
}
@Override
public E remove(int index){
if (index 0|| index >= size) throw new IndexOutOfBoundsException();
E removed = data[index];
for (int i = index; i size -1; i++){
data[i]= data[i +1];
}
data[--size]= null;
return removed;
}
}
i dont know why i keep getting these errors im so confused. and also why is my code underlined with red. plz help!!
MyArrayL... MyStrin... package MyImplementations;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!