Here is my lab which I am doing
And the code which I write is
package lab.pkg01;
import java.util.*;
public class Lab01 {
public static void main(String[] args) {
String[] L1 = {"Abu Dhabi", "Dubai", "Sharjah", "Ajman"};
String[] L2 = {"Fujara", "Dubai", "Ras Alkhaima", "AlAin"};
MyArrayList list1 = new MyArrayList(L1);
MyArrayList list2 = new MyArrayList(L2);
}
}
class MyArrayList{
static ArrayList<String> MyList = new ArrayList<>();
MyArrayList(String[] list){
for(String i:list){
MyList.add(i);
}
}
public static boolean contains(String e){
boolean b = false;
ArrayList<Integer> positions = new ArrayList<>();
for (int i=0; i<MyList.size(); i++){
if(e == MyList.get(i)){
positions.add(i);
b = true;
}
}
return b;
}
public boolean addAll(MyArrayList e){
for(String s: e){
if(MyList.contains(s)){
MyList.add(s);
} else {
}
}
return true;
}
public boolean removeAll(MyArrayList e){
for(String s: e){
if(!MyList.contains(s)){
MyList.remove(s);
} else {
}
}
return true;
}
@Override
public String toString(){
String s = "";
for (String i : MyList){
s = s+" "+i;
}
return "List: "+s;
}
}
It is giving an error
found: MyArrayList but reuired: array or java.lang object
on addAll() and removeAll() method. Can anyone help me out in resolving this.
Actually What I want to do is to compare Object created in main method to the MyList array so that I can perform addAll() and removeAll() methods.