-3

Kindly check the below code, This is showing some warning,

ArrayList<String[]> may not contain objects of String

ArrayList<String[]> mName = new ArrayList<>();
mValue = "John";
if (!mName.contains(mValue)) {

    String[] details = new String[]{"Driver", "Part-time"};
    mName.add(details);

}
4
  • 6
    if (!mName.contains(mValue)) { it contains String Arrays, so how can this work? Commented Jan 11, 2017 at 7:00
  • 3
    a List of String[] can't have Strings? the error tells you exactly what is wrong, read it Commented Jan 11, 2017 at 7:01
  • Your own title just says it all. You have a list of String array's, so how is that list ever going to contain a String object Commented Jan 11, 2017 at 7:13
  • 2
    I thought it was ArrayList<String> on the first look and, Sorry now understand the issue. Thanks for pointing out the problem. Commented Jan 11, 2017 at 9:04

2 Answers 2

2

Your if condition returns false because String[] and String are different types, so the equals method of String class will always return false. You must rethink your code. Please look also at this similar question: Using contains on an ArrayList with integer arrays

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out the problem
2

You have created ArrayList of String[] or String arrays. But you are checking that if ArrayList<String[]> mName contains a string. Java won't allow it. In this case you can check that your ArrayList cantains any String[] or not. So, if you want to check that string "John" is in the ArrayList then change

ArrayList<String[]> mName = new ArrayList<>();

to

ArrayList<String> mName = new ArrayList<>();

If you want to keep your previous code then change mValue to String array. Change

mValue = "John"

to

String[] mValue = new String[]{"John"};

N.B: mValue = "John" this line is missing a ;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.