0

Here is the thing. I have a character array as follows..

char[] modes = new char[] { 'm', 'q', 'h', 'y' };

Now I want to provide the user with the option to enter a character. If it exists in the modes array, I'll do the necessary. For that I used...

//to take a character as input
mode = input.next().charAt(0);
//now to check if the array contains the character
boolean ifExists = Arrays.asList(modes).contains(mode);

But strangely ifExists returns false.

  1. Any Ideas where am I doing wrong?
  2. If this is a bad way of doing it, please suggest a way.
6
  • "Strangely" on what input? I wouldn't find it strange if the input was a. Commented Jan 23, 2013 at 16:34
  • I suggest that you, on the code line before ifExists is evaluated print the value of mode and the value of all contents in the modes array. Probably, some value is not what you expect... Commented Jan 23, 2013 at 16:39
  • Why arent you using the binarySearch method on Arrays? Commented Jan 23, 2013 at 16:39
  • @Alex - my input was m Commented Jan 23, 2013 at 16:49
  • @Perception - i can always use binary search..but i am more eager to know if there is something in built. or something like that. Commented Jan 23, 2013 at 16:50

5 Answers 5

3

I think it's Autoboxing - the contains() method takes an object, not a primitive.

If you use Character instead of char it will work:

    Character[] modes = new Character[] { 'm', 'q', 'h', 'y' };

    //to take a character as input
    Character mode = "q123".charAt(0);
    //now to check if the array contains the character
    boolean ifExists = Arrays.asList(modes).contains(mode);

returns true

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

4 Comments

Won't autoboxing automatically kick in in this case? I'm curious as to why Java did not automatically wrap the primitives as objects.
It does kick in. Looking at the source code of contains(), it uses equals() under the hood, at that does work on autoboxed chars. I'm confused -Edit - Pat Burke has the answer
@Redandwhite - thanks for asking that. I too was curious about that.
Autoboxing doesn't kick in because Java doesn't autobox arrays. The parameter to Arrays.asList is T..., and T needs to be an Object type. For instance, Object[] array = new char[0]; will not compile.
3

The Arrays.asList() method is returning a List of char[] and not a List of char like you are expecting. I would recommend using the Arrays.binarySort() method like so:

    char[] modes = new char[] { 'm', 'q', 'h', 'y' };

    char mode = 'q';

    //now to check if the array contains the character
    int index = Arrays.binarySearch(modes, mode);
    boolean ifExists = index != -1;
    System.out.print(ifExists);

4 Comments

For the situation given, it works in all cases. Could you provide a case where it wold not work?
Actually, Arrays.binarySearch(modes, 'h') results in -1, where a negative value indicates the key is not in the array.
Interesting, must have overlooked that one! Good call.
Could just use Arrays.sort(modes) before searching it I suppose, but I don't know whether the order in modes is important in OP's program or not. (Seems like no, or he'd have been using indexOf instead of contains.) By the way, the docs for binarySearch also say it can return other negative values besides -1, so probably should use ifExists = index >= 0 or something to be a little more robust. Cheers,
1

I didn't find any problem with your code and try this,

If you use this kind of Colletions then you can do lots of operations using methods available defaultly...

List<Character> l = new ArrayList<Character>();
l.add('a');
l.add('b');
l.add('c');
System.out.println(l.contains('a'));

1 Comment

I know i can use a lot of things while working with collections..but i want to know how to do it when it is a primitive array.
1

You could just convert to a string and then run contains

new String(modes).contains("" + mode);

This should then return true or false for your primitive array

3 Comments

why concatenate that empty string? please explain.
Concatenating mode with an empty string just converts it to a string. Could also use String.valueOf(mode) for essentially the same result.
Thanks matts. yes I could use String.valueOf instead. I am converting more from chat to String
0

You could also use String indexOf:

boolean ifExists = new String(modes).indexOf(mode) >= 0;

or

boolean ifExists = "mqhy".indexOf(mode) >= 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.