Reputation: 47
Hi i am trying to add the values to list as show in below code. i am getting error.
for (String n2 : number ) {
List<String> ARRAY = new ArrayList<String>();
if (!ARRAY.contains(number)) {
ARRAY.add(number);
}}
But getting error while adding the number in to list.
error
java:271: error: no suitable method found for add(List<String>
ARRAY.add(number);
^
method List.add(int,String) is not applicable
(actual and formal argument lists differ in length)
method List.add(String) is not applicable
(actual argument List<String> cannot be converted to String by method invo
if i use like below
for (String n2 : number ) {
List<String> ARRAY = new ArrayList<String>();
if (!ARRAY.contains(n2)) {
Email(n2);
ARRAY.add(n2);
}
}
if i am using above. Though already email sent with value n2 again it is sending again. For first it has to sent but for second time since it is already in array it should not sent right?
Upvotes: 0
Views: 629
Reputation: 75
List<String> ARRAY = new ArrayList<String>();
for (String n2 : number ) {
if (!ARRAY.contains(n2)) {
ARRAY.add(n2);
}
}
List<String> ARRAY = new ArrayList<String>(); //have this out of the thread
Upvotes: 0
Reputation: 2081
number
is collection of string and you are adding int ARRAY
instead add n2
List<String> ARRAY = new ArrayList<String>();
for (String n2 : number ) {
if (!ARRAY.contains(n2)) {
ARRAY.add(n2);
}
}
Upvotes: 0
Reputation: 535
Problem with your code is you are adding number
instead of n2
Change the code like this
List<String> ARRAY = new ArrayList<String>();
for (String n2 : number ) {
if (!ARRAY.contains(n2)) {
ARRAY.add(n2);
}
}
Upvotes: 1
Reputation: 3266
I am afraid there is a bit more wrong with your code than just that one error. As has been pointed out many times, you are trying to add an iterable collection of strings, number
to your Array
rather than n2
which is the iteration variable. If you want to add complete Collection
instances you can do so using addAll()
.
As for the rest, I strongly recommend sticking to the Java naming convention and using lower case names for your variables. This will improve readability as many members of the community stick with that convention. You can find a neat write-up here.
You also seem to, unless your code is highly simplified, make the mistake of declaring an ArrayList
inside the scope of a loop. you are instantiating a new ArrayList
every time you enter the loop. I am not sure that is what you want to do. Be sure to check your design.
Also, if you simply want to avoid having duplicate values, I would suggest using Set
as it performs the check automatically using the hashCode()
of each member on insertion to check for collisions. Try doing:
HashSet<String> uniqueSet = new HashSet<>(number);
You should now have a Collection
of unique strings.
Upvotes: 0
Reputation: 26094
1) ARRAY should be outside of your for loop.
2) Replace if (!ARRAY.contains(number)) to if (!ARRAY.contains(n2 )).
Your code need to like this
List<String> ARRAY = new ArrayList<String>();
for (String n2 : number ) {
if (!ARRAY.contains(n2 )) {
ARRAY.add(n2);
}
}
Upvotes: 1
Reputation: 1284
You're trying to add a collection - numbers into a List of Strings. I am assuming you are trying to add n2 into ARRAY.
ARRAY.add(n2);
Upvotes: 0