“require: 数组或java.lang对象,找到:MyArrayList” 错误

huangapple go评论75阅读模式
英文:

"require: array or java.lang object, found: MyArrayList" error

问题

这是我正在进行的实验室:

“require: 数组或java.lang对象,找到:MyArrayList” 错误

代码如下:

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.equals(MyList.get(i))){
                positions.add(i);
                b = true;
            }
        }
        return b;
    }
    
    public boolean addAll(MyArrayList e){
        for(String s: e.MyList){
            if(MyList.contains(s)){
                MyList.add(s);
            } else {
            }
        }
        return true;
    }
    
    public boolean removeAll(MyArrayList e){
        for(String s: e.MyList){
            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;
    }
}

出现错误:

找到:MyArrayList,但需要:数组或java.lang对象

出现在addAll()和removeAll()方法。有人可以帮助我解决这个问题吗?

实际上,我想要做的是将主方法中创建的对象与MyList数组进行比较,以便我可以执行addAll()和removeAll()方法。

英文:

Here is my lab which I am doing
“require: 数组或java.lang对象,找到:MyArrayList” 错误

And the code which I write is

package lab.pkg01;
import java.util.*;
public class Lab01 { 
public static void main(String[] args) {
String[] L1 = {&quot;Abu Dhabi&quot;, &quot;Dubai&quot;, &quot;Sharjah&quot;, &quot;Ajman&quot;};
String[] L2 = {&quot;Fujara&quot;, &quot;Dubai&quot;, &quot;Ras Alkhaima&quot;, &quot;AlAin&quot;};
MyArrayList list1 = new MyArrayList(L1); 
MyArrayList list2 = new MyArrayList(L2);   
}  
}
class MyArrayList{
static ArrayList&lt;String&gt; MyList = new ArrayList&lt;&gt;();  
MyArrayList(String[] list){
for(String i:list){
MyList.add(i);
}
}
public static boolean contains(String e){
boolean b = false;
ArrayList&lt;Integer&gt; positions = new ArrayList&lt;&gt;();
for (int i=0; i&lt;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 = &quot;&quot;;
for (String i : MyList){
s = s+&quot; &quot;+i;
}
return &quot;List: &quot;+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.

答案1

得分: 1

首先,你不应该无缘无故地使用 static。我认为在这种情况下,你的 myListcontains() 方法是不需要使用 static 的。(参考链接:https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class)。在使用和处理静态成员时要非常小心。

另外,你的问题出在 for 循环上。你的字符串存储在名为 ArrayList myList 的地方,来自于 MyArrayList 类。另外,在你的 addAll() 方法中的 if(MyList.contains(s)) 应该改为 if(!MyList.contains(s)),因为你不想要添加重复项(我猜这只是你的笔误)。

所以,你的代码应该像这样修改:

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

另外,正如Aman所说,你应该使用 equals() 方法来比较字符串。另外,你的 removeAll()addAll() 方法目前只返回了 true,你可能也想修正一下这个问题!

英文:

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.(https://stackoverflow.com/questions/413898/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!

答案2

得分: 1

  1. 最好不要使用static列表。或者你必须在每次创建对象时清除它,这很丑陋。所以,首先将其更改为非static变量。

  2. 使用List<String> myList = new ArrayList<>(),而不是ArrayList<String> myList = new ArrayList<>()

  3. 在你的#contains()方法中,将Stringequals()进行比较,而不是==

  4. addAll似乎不起作用,因为在#addAll()#removeAll()for(String s: e)中,String不是MyArrayList

	public boolean addAll(String[] e){
       boolean isChanged = false;
		for(String s: e){
			if(!myList.contains(s)) {
				myList.add(s);
                isChanged = true;
            }
		}
		return isChanged;
	}
  1. 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. 最后,尝试使用Java Bean命名风格。例如,ArrayList<String> myList = new ArrayList<>(),用 myList 而不是 MyList
英文:
  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&lt;String&gt; myList = new ArrayList&lt;&gt;() instead of ArrayList&lt;String&gt; myList = new ArrayList&lt;&gt;().
  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&lt;String&gt; myList = new ArrayList&lt;&gt;(), myList not MyList.

huangapple
  • 本文由 发表于 2020年10月22日 18:29:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64480372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定