1

Here is my lab which I am doing Lab

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.

Hamid Ayub
  • 77
  • 5
  • 1
    First of all you `String` comparison is not good on `e == MyList.get(i)`. Strings should be compared with `equals()`. And a `static` will be used for the entire life time of the program execution. – Aman Oct 22 '20 at 11:04

2 Answers2

1

First of all, you shouldn't be using static for no reason. I don't think you need static for myList and the contains() method in this case.(What does the 'static' keyword do in a class?). Be very careful when using and dealing with static.

Also, Your issue is the for loop. Your Strings are stored in ArrayList myList, from the MyArrayList class. Plus if(MyList.contains(s)) in your addAll() method should be if(!MyList.contains(s)) , because you don't want to add duplicates (I'm assuming it's just your typo.

So instead of for example:

    public boolean addAll(MyArrayList e){
        for(String s: e){
            if(!MyList.contains(s)){
                MyList.add(s);
            }
        }
        return true;
    }

You should access your string through myList like so:

    public boolean addAll(MyArrayList e){
        for(String s: e.myList){
            if(!MyList.contains(s)){
                MyList.add(s);
            }
        }
        return true;
    }

Also, as Aman says you should use equals() to compare Strings. Additionally, your removeAll() and addAll() only returns true, so you may want to fix that as well!

Latios2217
  • 36
  • 3
  • Thanks for your answer. But when I used to remove static from the list, then it gives error in MyList.get(i) – Hamid Ayub Oct 22 '20 at 11:26
1
  1. It is better that you don't use static list. Or you have to clear it for every object creation, which is ugly. So, change that first to non static variable.
  2. List<String> myList = new ArrayList<>() instead of ArrayList<String> myList = new ArrayList<>().
  3. In your #contains() method compare String with equals() instead of ==.
  4. AddAll does not seem to work since String is not MyArrayList in for(String s: e) of #addAll() and #removeAll().
    public boolean addAll(String[] e){
       boolean isChanged = false;
        for(String s: e){
            if(!myList.contains(s)) {
                myList.add(s);
                isChanged = true;
            }
        }
        return isChanged;
    }
  1. Some modifications on removeAll.
    public boolean removeAll(String[] e){
        boolean isChanged = false;
        for(String s: e){
            if(myList.contains(s)) {
                myList.remove(s);
                isChanged  = true;
            }
        }
        return isChanged ;
    }
  1. Lastly, try to use Java bean naming style. e.g. ArrayList<String> myList = new ArrayList<>(), myList not MyList.
Aman
  • 1,627
  • 13
  • 19