英文:
"require: array or java.lang object, found: MyArrayList" error
问题
这是我正在进行的实验室:
代码如下:
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
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.
答案1
得分: 1
首先,你不应该无缘无故地使用 static
。我认为在这种情况下,你的 myList
和 contains()
方法是不需要使用 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
-
最好不要使用
static
列表。或者你必须在每次创建对象时清除它,这很丑陋。所以,首先将其更改为非static
变量。 -
使用
List<String> myList = new ArrayList<>()
,而不是ArrayList<String> myList = new ArrayList<>()
。 -
在你的
#contains()
方法中,将String
与equals()
进行比较,而不是==
。 -
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;
}
- 对
removeAll
进行一些修改。
public boolean removeAll(String[] e){
boolean isChanged = false;
for(String s: e){
if(myList.contains(s)) {
myList.remove(s);
isChanged = true;
}
}
return isChanged ;
}
- 最后,尝试使用Java Bean命名风格。例如,
ArrayList<String> myList = new ArrayList<>()
,用 myList 而不是 MyList。
英文:
- 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 nonstatic
variable. List<String> myList = new ArrayList<>()
instead ofArrayList<String> myList = new ArrayList<>()
.- In your
#contains()
method compareString
withequals()
instead of==
. AddAll
does not seem to work since String is notMyArrayList
infor(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;
}
- 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 ;
}
- Lastly, try to use Java bean naming style. e.g.
ArrayList<String> myList = new ArrayList<>()
, myList not MyList.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论