0

I am trying to check "using"the list contains () method to check for a character present in the array. The code seems to run but returns false.why is that?

import java.util.Arrays;

public class charcontain
{
    public static void main(String args[]) {
        char arr[]={'2','a','g','4'};

        System.out.println("4 in arr : "+ Arrays.asList(arr).contains('4'));
    }
}

4 in arr : false

vtu11710
  • 3
  • 3
  • 3
    Possible duplicate of [Arrays.asList() not working as it should?](https://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should). See also: [Varargs methods and primitive types](https://stackoverflow.com/questions/37098205/varargs-methods-and-primitive-types) – Michał Ziober Sep 11 '19 at 12:14

5 Answers5

2

You should check what Arrays.asList() returns to you.

It returns you List<char[]> instead of List<Character> because char is primitive. Try using Character[] like the following:

    Character arr[] = {'2', 'a', 'g', '4'};
    List<Character> characters = Arrays.asList(arr);
    System.out.println("4 in arr : "+ characters.contains('4'));
DDovzhenko
  • 1,295
  • 1
  • 15
  • 34
1
Arrays.asList(arr)

is not a List<Character>, it's a List<char[]>, with one element - the char[]. A List<char[]> doesn't contain a char, hence contains returns false.

The easiest approach would be to turn it to a String:

new String(arr).indexOf('4') >= 0
  // or
new String(arr).contains("4")

Some third-party libraries offer methods to convert a char[] to a List<Character>, for example Chars.asList in Guava.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

Because for char[] value Arrays.asList method creates List<char[]>. If you create Character[] array with same values, Arrays.asList method will return List<Character>:

import java.util.Arrays;
import java.util.List;

public class Main {

  public static void main(String[] args) {
    char[] primitives = {'2', 'a', 'g', '4'};
    List<char[]> listOfCharArrays = Arrays.asList(primitives);
    System.out.println("4 in arr : " + listOfCharArrays.contains('4'));

    Character[] arr = {'2', 'a', 'g', '4'};
    List<Character> chars = Arrays.asList(arr);
    System.out.println("4 in arr : " + chars.contains('4'));
  }
}

Above code prints:

4 in arr : false
4 in arr : true

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
1

Here's an approach based on this other question to use a Stream to avoid the conversion of arr to List. It's more verbose, but might perform better if the size of arr is large.

    char[] arr = {'2', 'a', 't', '4'};
    char search = '4';
    System.out.println(search + " in arr: " + CharBuffer.wrap(arr).chars()
        .mapToObj(i -> (char) i)
        .filter(c -> c == search)
        .findFirst()
        .isPresent());
Andrew S
  • 2,509
  • 1
  • 12
  • 14
0
 List<Character> listC = new ArrayList<Character>();
for (char c : chars) {
    listC.add(c);
} 
System.out.println("4 in arr : "+listC.contains('4'));
Amol Gharpure
  • 129
  • 1
  • 7